tags:

views:

225

answers:

2

This seems to me a simple question but I can't find the answer.

Currently I use an iterator to search through a vector and test its elements. I access the elements using

std::vector<int>::iterator it;

if(*it==0);

Can I use the same pointer arithmetic style logic to also test the next element (without altering my iterator)?

I first need to see if it will push the iterator out of bounds

if(it!=myvec.end())

Then test both current element and next element

if(*it==1 && *(it+1)==1)

Will this work as I expect from using pointers?

Many thanks in advance for your help.

+1  A: 

This will work indeed as vector iterator are random access iterator. That is not only can you act on them like you would do with pointers, but they are pretty much implemented using pointers / pointer arithmetic.

Gab Royer
+7  A: 

Yes, the iterators for std::vector are random access iterators so you add/subtract integral values to get other valid iterators.

Technically, it may not be pointer arithmetic, but they act just like pointers.

R Samuel Klatchko