在C语言中,使用++i和i++之间的区别是什么,在for循环的增量块中应该使用哪个?
当前回答
预填充是指在同一行上的增量。后增量是指行执行后的增量。
int j = 0;
System.out.println(j); // 0
System.out.println(j++); // 0. post-increment. It means after this line executes j increments.
int k = 0;
System.out.println(k); // 0
System.out.println(++k); // 1. pre increment. It means it increments first and then the line executes
当它带有OR和AND操作符时,它变得更有趣。
int m = 0;
if((m == 0 || m++ == 0) && (m++ == 1)) { // False
// In the OR condition, if the first line is already true
// then the compiler doesn't check the rest. It is a
// technique of compiler optimization
System.out.println("post-increment " + m);
}
int n = 0;
if((n == 0 || n++ == 0) && (++n == 1)) { // True
System.out.println("pre-increment " + n); // 1
}
在数组中
System.out.println("In Array");
int[] a = { 55, 11, 15, 20, 25 };
int ii, jj, kk = 1, mm;
ii = ++a[1]; // ii = 12. a[1] = a[1] + 1
System.out.println(a[1]); // 12
jj = a[1]++; // 12
System.out.println(a[1]); // a[1] = 13
mm = a[1]; // 13
System.out.printf("\n%d %d %d\n", ii, jj, mm); // 12, 12, 13
for (int val: a) {
System.out.print(" " + val); // 55, 13, 15, 20, 25
}
在c++中,指针变量的后/前增量
#include <iostream>
using namespace std;
int main() {
int x = 10;
int* p = &x;
std::cout << "address = " << p <<"\n"; // Prints the address of x
std::cout << "address = " << p <<"\n"; // Prints (the address of x) + sizeof(int)
std::cout << "address = " << &x <<"\n"; // Prints the address of x
std::cout << "address = " << ++&x << "\n"; // Error. The reference can't reassign, because it is fixed (immutable).
}
其他回答
++i(前缀操作):递增,然后赋值 (例如):int I = 5, int b = ++ I 在这种情况下,先将6赋值给b,然后递增到7,依此类推。
i++(后缀操作):赋值,然后递增 (例如):int I = 5, int b = i++ 在这种情况下,5首先被赋值给b,然后增加到6,依此类推。
在for循环的情况下:i++主要用于,因为通常我们在for循环中递增之前使用i的起始值。但是根据您的程序逻辑,它可能会有所不同。
我假设你现在已经理解了语义上的差异(尽管说实话我想知道为什么 人们会问“运算符X是什么意思”的问题,而不是阅读, 你知道的,一本书或网络教程之类的。
不管怎样,至于用哪个,忽略性能的问题 即使在c++中也不太重要。这是你做决定时应该遵循的原则 使用哪一种:
用代码表达你的意思。
如果语句中不需要value-before-increment,就不要使用这种形式的操作符。这是一个小问题,但除非你的风格指南禁止这样做 版本完全赞成其他的(又名愚蠢的风格指南),你应该使用 最准确地表达你要做的事情的形式。
QED,使用预增量版本:
for (int i = 0; i != X; ++i) ...
在循环中使用两者的有效结果是相同的。换句话说,循环将在两个实例中做完全相同的事情。
就效率而言,选择i++而不是++i可能会有一定的损失。根据语言规范,使用后增量操作符应该创建操作符所作用的值的额外副本。这可能是额外操作的来源。
但是,您应该考虑上述逻辑的两个主要问题。
Modern compilers are great. All good compilers are smart enough to realize that it is seeing an integer increment in a for-loop, and it will optimize both methods to the same efficient code. If using post-increment over pre-increment actually causes your program to have a slower running time, then you are using a terrible compiler. In terms of operational time-complexity, the two methods (even if a copy is actually being performed) are equivalent. The number of instructions being performed inside of the loop should dominate the number of operations in the increment operation significantly. Therefore, in any loop of significant size, the penalty of the increment method will be massively overshadowed by the execution of the loop body. In other words, you are much better off worrying about optimizing the code in the loop rather than the increment.
在我看来,整个问题可以归结为一种风格偏好。如果您认为预增量更具可读性,那么就使用它。就我个人而言,我更喜欢后增量,但这可能是因为这是我在了解优化之前学到的东西。
这是一个典型的过早优化的例子,像这样的问题有可能分散我们对设计中严肃问题的注意力。然而,这仍然是一个很好的问题,因为在“最佳实践”中没有统一的用法或共识。
++i将增加i的值,然后返回增加后的值。 I = 1; J = ++i; (i = 2, j = 2) i++将增加I的值,但返回I被增加前的原始值。 I = 1; J = i++; (i = 2, j = 1)
对于For循环,两者都可以。++i似乎更常见,也许是因为这是K&R中使用的。
在任何情况下,遵循“优先选择++i而不是i++”的指导方针,您都不会出错。
这里有一些关于++i和i++的效率的评论。在任何非学生项目的编译器中,都不会有性能差异。您可以通过查看生成的代码来验证这一点,它们是相同的。
效率问题很有趣……以下是我试图回答的问题: 在C语言中i++和++i之间有性能差异吗?
正如@OnFreund指出的那样,对于c++对象是不同的,因为操作符++()是一个函数,编译器不知道优化掉一个临时对象的创建来保存中间值。
++i:是增量前,另一个是增量后。
i++:获取元素,然后对其进行递增。 ++i:增加i,然后返回元素。
例子:
int i = 0;
printf("i: %d\n", i);
printf("i++: %d\n", i++);
printf("++i: %d\n", ++i);
输出:
i: 0
i++: 0
++i: 2