Position of iterator in vector?

Quick question, if I have a vector getting looped through by an iterator, how do I say:

if(iterator is at this position in the vector)
{
do something
}

I though using if(vector.at(1)) would work but thats just if there is something in that position, not if the iterator is at it.
Something like this?
1
2
3
4
5
6
#include <iterator>

if(std::distance(vec.begin(), my_iterator) == certain_position)
{
    // deal with it
}      
If your code depends on the iterator being a vector iterator you should probably just iterate over it using an index, achieves the same thing with much less of a hassle. The primary use for iterators is writing collection agnostic code (actually, it even abstracts from the fact that there is a collection).
Last edited on
Before an iterator is used it is set to some valid value. Also if a sequence of itteration is performed then the following check is usually applied

if ( MyIterator != v.end() )
Thanks for the help,but I decided to just cheat and use a counter :P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void foo( const std::vector<int>& seq )
{
    for( auto iterator = seq.begin() ; iterator != seq.end() ; ++iterator )
    {
        auto position = iterator - seq.begin() ;
        std::cout << seq[position] << '\n' ;
    }

    for( std::size_t position = 0 ; position < seq.size() ; ++position )
    {
        auto iterator = seq.begin() + position ;
        std::cout << *iterator << '\n' ;
    }
}
@JLBorges ,

to make the code more readable I would change it the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void foo( const std::vector<int>& seq )
{
    for( auto iterator = seq.begin() ; iterator != seq.end() ; ++iterator )
    {
        auto position = std::distance( sec.begin(), iterator ) ;
        std::cout << seq[position] << '\n' ;
    }

    for( std::size_t position = 0 ; position < seq.size() ; ++position )
    {
        auto iterator = std::next( seq.begin(), position ) ;
        std::cout << *iterator << '\n' ;
    }
}
Meh, I don't think it makes it that much more readable. I prefer JL's version.
The first example makes the code more dependable. I prefer to deal with less dependable code.
I don't think you mean "dependable" here :)

There really aren't any dependencies that you'd need to avoid when you're already set on using a vector. I'm not even sure if distance works when '-' doesn't.
> to make the code more readable I would change it the following way

IMHO, it doesn't make it any more readable.

It is however required if there is no certainty that the iterator is a random access iterator; so the code does become a bit more maintainable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template < typename SEQUENCE_CONTAINER > void foo( const SEQUENCE_CONTAINER& seq )
{
    for( auto iterator = seq.begin() ; iterator != seq.end() ; ++iterator )
    {
        auto position = std::distance( seq.begin(), iterator ) ;
        std::cout << seq[position] << '\n' ;
    }

    for( std::size_t position = 0 ; position < seq.size() ; ++position )
    {
        auto iterator = std::next( seq.begin(), position ) ;
        std::cout << *iterator << '\n' ;
    }
}


EDIT: On second thoughts, you wouldn't want to write this kind of code (quadratic time) unless you know that the iterator is a random access iterator.
Last edited on
@JLBorges,
you wouldn't want to write this kind of code (quadratic time) unless you know that the iterator is a random access iterator.


Many standard algorithms uses this kind of code for forward iterators.
> Many standard algorithms uses this kind of code for forward iterators.

This kind seq[position] ???
As I understand you was speaking about std::distance( seq.begin(), iterator ) before. But now you have started to speak about seq[position].
> As I understand you was speaking about std::distance( seq.begin(), iterator ) before.
> But now you have started to speak about seq[position].

Speaking about std::distance( seq.begin(), iterator ) and ignoring the superficially inconvenient seq[position] doesn't change anything.

If the terator is not a random access iterator, std::distance( seq.begin(), iterator ) is O(N), doing it in a loop that executes O(N) times still makes it O(N2) or quadratic.
Last edited on
I already said that many standard algorithms which deal with forward or bidirectional iterators uses std::distance. Even where random access iterators is used this function are applied. I advice to look through realizations of the standard algorithms. if you do not understand why this function is used it does not mean that it shall not be used.
> if you do not understand why this function is used it does not mean that it shall not be used.

Because you do not understand when, where and why this function is used, it does not mean that it should always be used.
One more look through standard algorithm realizations and you will know many interesting for you.
Topic archived. No new replies allowed.