简体   繁体   中英

Concept difference between pre and post increment operator for STL

Supposedly:

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

I do 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 here between ++iter and iter++ ? (Bear in mind that I do know that both yield the same result here).

++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.

It means the same as integer.

For pre-increment, iter is incremented, and the returned object is the same as iter.

For post-increment, iter must be copied to a temporary, then iter is incremented, the copy is returned. However, most compilers can optimize out the fact that this copy isn't used, and thus the copy can be eliminated, making it the same as pre-increment.

For those compilers that can't, post-increment might result in slightly slower preformance, but thats not generally the case anymore.

It all depends on how they are implemented.

But the most common way implementation of post increment is in terms of 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;
    }
};

So the standard implementation of post increment calls pre-increment and in addition makes a copy of the object. Note there is also an additional copy construction on return but this is usually illeded by the compiler.

Note pre-increment because it affects the same obejct usually returns a reference to itslef (so not cost on the return).

In general pre-increment is usually preferable to post-increment because it may allow for some optimization that can avoid temporaries being constructed. As to exactly how it's implemented that depends on the STL included with your compiler.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM