Please check the commented line of code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int>numbers{1,2,3,4,5,6,7,8};
vector<int>::iterator it, beg=numbers.begin(), end=numbers.end();
for(it=beg; it!=end; it++){
cout<<*it++<<endl; //THIS LINE PRINTS 1 3 5 7
}
return 0;
}
I'm reading about iterators and trying some things. That line seems to print the element it
refers to, then increment it
. In fact it produces the same results as:
cout<<*it<<endl;
it++;
I didn't explain it clearly, the real question is: can you perform 2 operations on an iterator like that?
And why *(it+1)
is different than *(it++)
?
Thanks.