Position of iterator in vector?

May 18, 2012 at 1:12pm
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.
May 18, 2012 at 1:41pm
Something like this?
1
2
3
4
5
6
#include <iterator>

if(std::distance(vec.begin(), my_iterator) == certain_position)
{
    // deal with it
}      
May 18, 2012 at 1:48pm
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 May 18, 2012 at 1:48pm
May 18, 2012 at 1:56pm
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() )
May 18, 2012 at 2:06pm
Thanks for the help,but I decided to just cheat and use a counter :P
May 18, 2012 at 3:18pm
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' ;
    }
}
May 18, 2012 at 3:42pm
@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' ;
    }
}
May 18, 2012 at 3:44pm
Meh, I don't think it makes it that much more readable. I prefer JL's version.
May 18, 2012 at 3:50pm
The first example makes the code more dependable. I prefer to deal with less dependable code.
May 18, 2012 at 3:58pm
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.
May 18, 2012 at 3:59pm
> 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 May 18, 2012 at 4:05pm
May 18, 2012 at 4:09pm
@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.
May 18, 2012 at 4:14pm
> Many standard algorithms uses this kind of code for forward iterators.

This kind seq[position] ???
May 18, 2012 at 4:45pm
As I understand you was speaking about std::distance( seq.begin(), iterator ) before. But now you have started to speak about seq[position].
May 18, 2012 at 4:55pm
> 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 May 18, 2012 at 4:56pm
May 18, 2012 at 5:10pm
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.
May 18, 2012 at 5:17pm
> 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.
May 18, 2012 at 5:21pm
One more look through standard algorithm realizations and you will know many interesting for you.
Topic archived. No new replies allowed.