后缀和前缀增量运算符 [英] postfix and prefix increment operator in a for 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);
}
}
和
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);
}
}
当 col = 0
,in ex.1 输出[col] [row]
$ c>和In ex.2 输出[col] [row]
将 。我是对吗?
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 ?
问题2:使用>> = 1U
而不是 / = 2
有什么不同?
Question 2 : Would using >>= 1U
instead of /= 2
make any difference ?
推荐答案
col
- 假设 col
是原始值。如果 col
是一个类,前缀和后缀'++'运算符可能被重载以做两个不同的事情,虽然我会认为它的坏习惯。请考虑以下示例:
It does not make any difference to the value of col
within the loop - assuming col
is a primitive value. If col
was 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:
#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;
}
}
到9,尽管事实上你预先增加一个,后增量在另一个。 i
的增量发生在循环的每次运行结束时,无论是使用预增量还是后增量。我相信预递增是更有效的,因为 - 我可能错了这里编译器然后不需要使用一个临时变量 1。,但这只会是明显的,如果你循环很长一段时间(当然,更多的计算罪是以效率的名义而不是任何其他单一原因 a>。)
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 i
happens 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'.)
至于问题2:
问题2:使用>> = 1U
而不是= / 2会有什么区别吗?
Question 2 : Would using >>= 1U instead of =/2 make any difference ?
如果编译器没有优化,则位移会更快,但是有可能你的编译器会将其优化为位移。
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.
另外,我一般认为 unsigned variableName
(即,删除 int
)不好的做法 - 虽然C ++将推送 int
任何一个缺少,它对我来说不太容易阅读。
As a side note, I generally find doing unsigned variableName
(that is, dropping the int
) bad practice - although C++ will shove in an int
anywhere one is missing, it is less readable to me.
1。:Stephen (a different Stephen;))注意到 - 预增量对于标准库容器迭代器更有效,但是对于原始类型没有什么不同,因为复制一个整数比复制一个更大的迭代器特别是std :: set和std :: map迭代器)。
1.: Stephen in the comments (a different Stephen ;) ) 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)."
这篇关于后缀和前缀增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!