Conceptual difference between pre and post increment statement for STL

Supposedly:

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

I understand the difference when it comes to pre / post increment for built-in types like int , etc., but in terms of an iterator, what's the difference between ++iter and iter++ ? (Keep in mind that I know that both give the same result here).

+6
source share
5 answers

The difference is that they do not produce the same result, while this particular example will do the same regardless of the form of increment 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 preceding the increment. Usually this is not necessary for fundamental types, but for things like iterators, to create a temporary value, a non-incremental value is required for subsequent return.

+1
source

++iter will most likely be faster, but not slower than iter++ .

The implementation of the post increment iter++ operator requires the generation of an additional temporary (this temporary returns when the original iter increases ++ )) when implementing the post increment ++iter operator, therefore, if the compiler cannot optimize (yes, it can) write increment, then ++iter will most likely be faster than iter++ .

Given the above, it is always recommended to use ++iter in a loop.

+9
source

This means the same as an integer.

For a preliminary increment, iter is incremented, and the returned object is the same as iter.

For a post-increment, iter must be copied to a temporary one, then the itera is incremented, the copy is returned. However, most compilers can optimize the fact that this copy is not used, and thus the copy can be eliminated, which makes it the same as pre-increment.

For those compilers that cannot, post-increment can lead to slightly slower preprocessing, but this is not so common.

+4
source

Usually, a pre-increment is usually preferable to a post-increment, since it can allow some optimization, which can avoid creating temporary objects. As for exactly how this is implemented, it depends on the STL included in your compiler.

+2
source

It all depends on how they are implemented.

But the most common way to implement post-increment is to pre-increment with an extra copy.

 class MyIter { // Definition of pre-increment: // ++object; MyIter& operator++() { /* Increment the iterator as appropriate You should be changing the object in place */ // Once you are done return yourself. return *this; } // Definition of post-increment: // object++; MyIter operator++(int) { // Post increment (returns the same value) so build the result. MyIter result(*this); // Now do the increment using pre-increment on the current object ++(*this); // return the result. return result; } }; 

Thus, the standard implementation of post increment causes a preliminary increment and, in addition, makes a copy of the object. Note that there is also an additional copy-on-return construct, but this is usually compromised.

Pay attention to the preliminary increment, because it affects the same obejct, as a rule, returns a link to itslef (therefore, it is not worth returning).

+2
source

Source: https://habr.com/ru/post/897419/


All Articles