Concept difference between pre and post increment operator for STL

后端 未结 5 1421
再見小時候
再見小時候 2020-12-19 04:44

Supposedly:

for (vector::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
{}

I do understand the difference when it com

5条回答
  •  时光说笑
    2020-12-19 05:23

    The difference is they do not yield the same result, while this particular example will do the same regardless of the increment form used. The pre-increment form first increments the value, and then returns it; while the post-increment form increments the result but returns the value previous to incrementation. This is usually a no cost for fundamental types, but for things like iterators it requires creating a temporary value to hold the non-incremented value, in order to be later returned.

提交回复
热议问题