Concept difference between pre and post increment operator for STL

后端 未结 5 1448
再見小時候
再見小時候 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:38

    ++iter is most likely to be faster but never slower than iter++.

    Implementing the post increment operator iter++ needs to generate an additional temporary(this temporary is returned back while the original iter is incremented ++) over implementing the post increment operator ++iter, So unless the compiler can optimize(yes it can) the post increment, then ++iter will most likely be faster than iter++.

    Given the above, It is always preferable to use ++iter in the looping conditions.

提交回复
热议问题