-2

sometimes I see

for(vector<int>::iterator it=a.begin();it!=a.end();it++){
}

and sometimes

for(vector<int>::iterator it=a.begin();it!=a.end();++it){
}

It seems no difference when I try to print some vector to test, but not sure if it has some difference in side effect, is there any difference between them?

bashrc
  • 4,725
  • 1
  • 22
  • 49
ggrr
  • 7,737
  • 5
  • 31
  • 53

1 Answers1

1

In your question you copied the same code, however the title indicates your confusion lies in pre-increment (++it) vs. post-increment (it++). There actually is an important difference here: The pre-increment operator will increment it and return the incremented value. The post-increment operator will also increment it but will return the old value. The same logic applies to the pre and post decrement operators (--it) and (it--).

However, in a for loop, the behavior will be the same regardless of which (pre or post) you use.

dillonh2
  • 155
  • 2
  • 12