C++ 后增量和前增量的概念?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4445706/
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
Post-increment and Pre-increment concept?
提问by Saad Masood
I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?
我不明白后缀和前缀递增或递减的概念。谁能给出更好的解释?
回答by Cheers and hth. - Alf
All four answers so far are incorrect, in that they assert a specific order of events.
到目前为止,所有四个答案都是不正确的,因为它们断言了特定的事件顺序。
Believing that "urban legend" has led many a novice (and professional) astray, to wit, the endless stream of questions about Undefined Behavior in expressions.
相信“都市传说”让很多新手(和专业人士)误入歧途,也就是说,关于表达式中未定义行为的问题层出不穷。
So.
所以。
For the built-in C++ prefix operator,
对于内置的 C++ 前缀运算符,
++x
increments x
and produces (as the expression's result) x
as an lvalue, while
递增x
并产生(作为表达式的结果)x
作为左值,而
x++
increments x
and produces (as the expression's result) the original value of x
.
递增x
并产生(作为表达式的结果) 的原始值x
。
In particular, for x++
there is no no time orderingimplied for the increment and production of original value of x
. The compiler is free to emit machine code that produces the original value of x
, e.g. it might be present in some register, and that delays the increment until the end of the expression (next sequence point).
特别是,x++
没有任何时间顺序隐含的原始值的增加和生产x
。编译器可以自由地发出产生 的原始值的机器代码x
,例如它可能存在于某个寄存器中,并将增量延迟到表达式结束(下一个序列点)。
Folks who incorrectly believe the increment must come first, and they are many, often conclude from that certain expressions must have well defined effect, when they actually have Undefined Behavior.
错误地认为增量必须首先出现的人很多,他们经常从某些表达式必须具有明确定义的效果中得出结论,而实际上它们具有未定义的行为。
回答by sje397
int i, x;
i = 2;
x = ++i;
// now i = 3, x = 3
i = 2;
x = i++;
// now i = 3, x = 2
'Post' means after - that is, the increment is done after the variable is read. 'Pre' means before - so the variable value is incremented first, then used in the expression.
'Post' 表示之后 - 即在读取变量后完成增量。'Pre' 表示之前 - 所以变量值首先增加,然后在表达式中使用。
回答by wilhelmtell
The difference between the postfixincrement, x++
, and the prefixincrement, ++x
, is precisely in howthe two operators evaluate their operands. The postfix increment conceptually copies the operand in memory, increments the original operand and finally yields the value of the copy. I think this is best illustrated by implementing the operator in code:
后缀增量x++
和前缀增量之间的区别++x
恰恰在于两个运算符如何评估其操作数。后缀递增在概念上复制内存中的操作数,递增原始操作数并最终产生副本的值。我认为这最好通过在代码中实现运算符来说明:
int operator ++ (int& n) // postfix increment
{
int tmp = n;
n = n + 1;
return tmp;
}
The above code will not compile because you can't re-define operators for primitive types. The compiler also can't tell here we're defining a postfixoperator rather than prefix, but let's pretend this is correct and valid C++. You can see that the postfix operator indeed acts on its operand, but it returns the old value prior to the increment, so the result of the expression x++
is the value prior to the increment. x
, however, isincremented.
上面的代码将无法编译,因为您无法为原始类型重新定义运算符。编译器也无法判断我们在这里定义的是后缀运算符而不是prefix,但让我们假设这是正确且有效的 C++。可以看到后缀运算符确实作用于它的操作数,但它返回的是增量之前的旧值,因此表达式的结果x++
是增量之前的值。x
,然而,是递增的。
The prefix increment increments its operand as well, but it yields the value of the operand afterthe increment:
前缀增量也增加其操作数,但它在增量后产生操作数的值:
int& operator ++ (int& n)
{
n = n + 1;
return n;
}
This means that the expression ++x
evaluates to the value of x
afterthe increment.
这意味着表达式++x
计算为增量x
后的值。
It's easy to think that the expression ++x
is therefore equivalent to the assignmnet (x=x+1)
. This is not precisely so, however, because an incrementis an operation that can mean different things in different contexts. In the case of a simple primitive integer, indeed ++x
is substitutable for (x=x+1)
. But in the case of a class-type, such as an iterator of a linked list, a prefix increment of the iterator most definitely does not mean "adding one to the object".
因此,很容易认为该表达式++x
等效于 assignmnet (x=x+1)
。然而,事实并非如此,因为增量是一种在不同上下文中可能意味着不同事物的操作。在一个简单的原始整数的情况下,确实++x
可以替代(x=x+1)
。但是在类类型的情况下,例如链表的迭代器,迭代器的前缀增量绝对不意味着“向对象添加一个”。
回答by mathewbruens
No one has answered the question: Why is this concept confusing?
没有人回答这个问题: 为什么这个概念令人困惑?
As an undergrad Computer Science major it took me awhile to understand this because of the way I readthe code.
作为一名本科计算机科学专业,由于我阅读代码的方式,我花了一段时间才理解这一点。
The following is not correct!
以下说法不正确!
x = y++
x = y++
X is equal to y postincrement. Which would logically seem to mean X is equal to the value of Y afterthe increment operation is done. Postmeaning after.
X 等于 y后增量。这在逻辑上似乎意味着在完成增量操作后X 等于 Y 的值。帖子的意思后。
or
或者
x = ++y
X is equal to y pre-increment. Which would logically seem to mean X is equal to the value of Y beforethe increment operation is done. Premeaning before.
x = ++y
X 等于 y预增量。这在逻辑上似乎意味着在完成增量操作之前X 等于 Y 的值。预意思之前。
The way it works is actually the opposite. This concept is confusing because the language is misleading. In this case we cannot use the words to define the behavior.
x=++y is actually read as X is equal to the value of Y afterthe increment.
x=y++ is actually read as X is equal to the value of Y beforethe increment.
它的工作方式实际上是相反的。这个概念令人困惑,因为语言具有误导性。在这种情况下,我们不能用词来定义行为。
x=++y 实际上读作 X 等于增量后的Y 值。
x=y++ 实际上读作 X 等于增量前的Y 值。
The words pre and post are backwards with respect to semantics of English. They only mean where the ++ is in relation Y. Nothing more.
pre 和 post 这两个词在英语的语义上是倒退的。它们仅表示 ++ 与 Y 的关系。仅此而已。
Personally, if I had the choice I would switch the meanings of ++y and y++. This is just an example of a idiom that I had to learn.
就我个人而言,如果我可以选择,我会转换 ++y 和 y++ 的含义。这只是我必须学习的一个习语的例子。
If there is a method to this madness I'd like to know in simple terms.
如果有一种方法可以解决这种疯狂,我想用简单的术语来了解。
Thanks for reading.
谢谢阅读。
回答by Jonathan Wood
It's pretty simple. Both will increment the value of a variable. The following two lines are equal:
这很简单。两者都会增加变量的值。以下两行是相等的:
x++;
++x;
The difference is if you are using the value of a variable being incremented:
不同之处在于,如果您使用的是递增的变量的值:
x = y++;
x = ++y;
Here, both lines increment the value of y by one. However, the first one assigns the value of y before the increment to x, and the second one assigns the value of y after the increment to x.
这里,两行都将 y 的值加一。但是,第一个将增量之前的 y 值赋给 x,第二个将增量之后的 y 值赋给 x。
So there's only a difference when the increment is also being used as an expression. The post-increment increments after returning the value. The pre-increment increments before.
所以只有当增量也被用作表达式时才有区别。后增量在返回值后递增。pre-increment 增加之前。
回答by Seth
int i = 1;
int j = 1;
int k = i++; // post increment
int l = ++j; // pre increment
std::cout << k; // prints 1
std::cout << l; // prints 2
Post increment implies the value i
is incremented after it has been assigned to k
. However, pre increment implies the value j is incremented before it is assigned to l
.
后增量意味着该值i
在分配给 后增加k
。但是,预增量意味着值 j 在分配给 之前增加l
。
The same applies for decrement.
这同样适用于递减。
回答by Olian04
Since we now have inline javascript snippets I might as well add an interactive example of pre and pos increment. It's not C++ but the concept stays the same.
由于我们现在有内联 javascript 片段,我不妨添加一个 pre 和 pos 增量的交互式示例。它不是 C++,但概念保持不变。
let A = 1;
let B = 1;
console.log('A++ === 2', A++ === 2);
console.log('++B === 2', ++B === 2);
回答by drvenom
Post increment(a++)
后增量(a++)
If int b = a++,then this means
如果 int b = a++,那么这意味着
int b = a;
a = a+1;
Here we add 1 to the value. The value is returned before the increment is made,
在这里,我们将值加 1。在进行增量之前返回该值,
For eg a = 1; b = a++;
例如 a = 1; b = a++;
Then b=1 and a=2
然后 b=1 和 a=2
Pre-increment(++a)
预增量(++a)
If int b = ++a; then this means
如果 int b = ++a; 那么这意味着
a=a+1;
int b=a ;
Pre-increment: This will add 1 to the main value. The value will be returned after the increment is made, For a = 1; b = ++a; Then b=2 and a=2.
预增量:这会将主值加 1。递增后返回值, For a = 1; b = ++a; 然后 b=2 和 a=2。
回答by hypergogeta
Post-increment:
后增量:
int x, y, z;
x = 1;
y = x++; //this means: y is assigned the x value first, then increase the value of x by 1. Thus y is 1;
z = x; //the value of x in this line and the rest is 2 because it was increased by 1 in the above line. Thus z is 2.
Pre-increment:
预增量:
int x, y, z;
x = 1;
y = ++x; //this means: increase the value of x by 1 first, then assign the value of x to y. The value of x in this line and the rest is 2. Thus y is 2.
z = x; //the value of x in this line is 2 as stated above. Thus z is 2.
回答by venkat
Post-increment(x++) increament happens at next statement:
后增量(x++)增量发生在下一条语句:
Example for Post-increament:
后增量示例:
static void Main(string[] args)
{
int x = 0;
int y= Method(x++);//x=0
Console.WriteLine(x);// now x=1
Console.WriteLine(y);// but y=0;
}
public static int Method(int x)
{
//when called value of x=0;
return x;//returns 0
}
Pre_increament(++x) increament happens at current statement
Pre_increament(++x) 增加发生在当前语句
Example for Pre-increament:
预增示例:
static void Main(string[] args)
{
int x = 0;
int y= Method(++x);//x=1
Console.WriteLine(x);// now also x=1
Console.WriteLine(y);//y is 1
}
public static int Method(int x)
{
//inside x=1;
return x; //returns 1
}