简体   繁体   English

STL的前置和后置增量运算符之间的概念差异

[英]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++ ? 我确实了解内置类型(如int等)的前/后递增的区别,但是就迭代器而言, ++iteriter++之间有什么区别? (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++ . ++iter最有可能比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++ . 实现后增量运算符iter++比实现后增量运算符++iter生成一个额外的临时文件(当原始iter递增++时,此临时文件将返回),因此除非编译器可以优化(是的)增量,则++iter最有可能比iter++更快。

Given the above, It is always preferable to use ++iter in the looping conditions. 鉴于以上所述,在循环条件下始终最好使用++iter

It means the same as integer. 它的含义与整数相同。

For pre-increment, iter is incremented, and the returned object is the same as iter. 对于pre-increment,iter会增加,并且返回的对象与iter相同。

For post-increment, iter must be copied to a temporary, then iter is incremented, the copy is returned. 对于后增量,必须将iter复制到临时副本,然后将iter递增,并返回副本。 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. 至于确切的实现方式,取决于编译器随附的STL。

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. 对于基本类型,这通常是免费的,但是对于迭代器之类的东西,它需要创建一个临时值来保存非增量值,以便以后返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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