C++ for循环中的后缀和前缀增量运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3541399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:04:55  来源:igfitidea点击:

postfix and prefix increment operator in a for loop

c++loops

提问by Ahmed

Possible Duplicate:
Difference between i++ and ++i in a loop?

可能的重复:
循环中 i++ 和 ++i 之间的区别?

Can anyone explain what's the difference between those:

谁能解释一下它们之间的区别:

for(unsigned col = 0; col < n; ++col, num_to_fill >>= 1U)
{

    for(unsigned row = num_to_fill; row < (1U << n); row += (num_to_fill * 2))
    {
        std::fill_n(&output[col][row], num_to_fill, 1);
    }
}

and

for(unsigned col = 0; col < n; col++, num_to_fill >>= 1U)
{

    for(unsigned row = num_to_fill; row < (1U << n); row += (num_to_fill * 2))
    {
        std::fill_n(&output[col][row], num_to_fill, 1);
    }
}

When col=0, In ex.1 Output[col][row]will be output[1][row]and In ex.2 Output[col][row]will be output[0][row]. Am I right ?

当 时col=0,在 ex.1Output[col][row]中将是output[1][row],在 ex.2Output[col][row]中将是output[0][row]。我对吗 ?

Question 2 : Would using >>= 1Uinstead of /= 2make any difference ?

问题 2:使用>>= 1U而不是有/= 2什么区别吗?

回答by Stephen

It does not make any difference to the value of colwithin the loop - assuming colis a primitive value. If colwas a class, the prefix and postfix '++' operators might be overloaded to do two different things, although I would consider it bad practice. Consider the following example:

它对col循环内的值没有任何影响- 假设col是原始值。如果col是一个类,前缀和后缀“++”运算符可能会被重载以做两种不同的事情,尽管我认为这是不好的做法。考虑以下示例:

#include <iostream>

using namespace std;

int main() {
    for(int i = 0; i < 10; i++) {
        cout << i << endl;
    }

    cout << endl;

    for(int i = 0; i < 10; ++i) {
        cout << i << endl;
    }

}

Both of these just print out 0 to 9, despite the fact that you pre-increment in one, and post-increment in the other. The incrementation of ihappens at the end of each run of the loop whether or not you use pre or post increment. I believe pre-incrementing is more efficient, since - and I may be wrong here - the compiler does not then need to use a temporary variable1., but this would only be noticeable if you are looping for a very long time (and of course 'More computing sins are committed in the name of efficiency than for any other single reason'.)

这两个都只是打印出 0 到 9,尽管您在一个中预先递增,而在另一个中后递增。i无论您是否使用前增量或后增量,的增量都发生在循环的每次运行结束时。我相信预递增更有效,因为 - 我在这里可能是错的 - 编译器不需要使用临时变量1.,但这只会在您循环很长时间(并且当然“以效率的名义犯下的计算罪比任何其他单一原因都多”。)

As for question 2:

至于问题2:

Question 2 : Would using >>= 1U instead of =/2 make any difference ?

问题 2:使用 >>= 1U 而不是 =/2 会有什么不同吗?

Unlikely. Bit shifting would be faster if the compiler did not optimise, but chances are that your compiler will optimise this into a bit shift.

不太可能。如果编译器没有优化,位移会更快,但您的编译器可能会将其优化为位移。

As a side note, I generally find doing unsigned variableName(that is, dropping the int) bad practice - although C++ will shove in an intanywhere one is missing, it is less readable to me.

作为旁注,我通常会发现做unsigned variableName(即放弃int)不好的做法 - 尽管 C++ 会在int任何缺少的地方推挤,但对我来说可读性较差。

1.: Stephen in the comments (a differentStephen ;) ) notes that - "Pre-increment is more efficient for standard library container iterators, but it's no different for primitive types, since copying an integer is cheaper than copying a larger iterator (in particular std::set and std::map iterators)."

1.:斯蒂芬评价(一个不同斯蒂芬;))指出, - “预增量为标准库容器迭代器更有效,但它的基本类型没有什么不同,因为复制的整数比复制的较大迭代器更便宜(特别是 std::set 和 std::map 迭代器)。”

回答by jpalecek

There is no difference for unsigned. However, there would be a difference for classes with overloaded operator++, because it would call its different overloads (usually, the postfix operator creates a copy of the class, which means it may be slower).

没有区别unsigned。但是,具有重载的类会有所不同operator++,因为它会调用不同的重载(通常,后缀运算符会创建类的副本,这意味着它可能会更慢)。

Would using >>= 1U instead of /=2 make any difference?

使用 >>= 1U 而不是 /=2 会有什么不同吗?

Probably not. Its semantics are the same for unsigned, and the compilers usually treat them equally and can change one into another if it's faster.

可能不是。它的语义与 unsigned 相同,编译器通常平等对待它们,如果速度更快,可以将它们转换为另一种。