5.5. Increment and Decrement Operators5.5. 自增和自减操作符The increment (++) and decrement (--) operators provide a convenient notational shorthand for adding or subtracting 1 from an object. There are two forms of these operators: prefix and postfix. So far, we have used only the prefix increment, which increments its operand and yields the changed value as its result. The prefix decrement operates similarly, except that it decrements its operand. The postfix versions of these operators increment (or decrement) the operand but yield a copy of the original, unchanged value as its result: 自增(++)和自减(--)操作符为对象加1或减1操作提供了方便简短的实现方式。它们有前置和后置两种使用形式。到目前为止,我们已经使用过前自增操作,该操作使其操作数加1,操作结果是修改后的值。同理,前自减操作使其操作数减 1。这两种操作符的后置形式同样对其操作数加 1(或减 1),但操作后产生操作数原来的、未修改的值作为表达式的结果: int i = 0, j; j = ++i; // j = 1, i = 1: prefix yields incremented value j = i++; // j = 1, i = 2: postfix yields unincremented value Because the prefix version returns the incremented value, it returns the object itself as an lvalue. The postfix versions return an rvalue. 因为前置操作返回加1后的值,所以返回对象本身,这是左值。而后置操作返回的则是右值。 Postfix Operators Return the Unincremented Value后置操作符返回未加1的值The postfix version of ++ and -- is used most often when we want to use the current value of a variable and increment it in a single compound expression: 当我们希望在单个复合表达式中使用变量的当前值,然后再加1时,通常会使用后置的 ++ 和 -- 操作: vector<int> ivec; // empty vector int cnt = 10; // add elements 10...1 to ivec while (cnt > 0) ivec.push_back(cnt--); // int postfix decrement This program uses the postfix version of -- to decrement cnt. We want to assign the value of cnt to the next element in the vector and then decrement cnt before the next iteration. Had the loop used the prefix version, then the decremented value of cnt would be used when creating the elements in ivec and the effect would be to add elements from 9 down to 0. 这段程序使用了后置的 -- 操作实现 cnt 减 1。我们希望把 cnt 的值赋给 vector 对象的下一个元素,然后在下次迭代前 cnt 的值减 1。如果在循环中使用前置操作,则是用 cnt 减 1 后的值创建 ivec 的新元素,结果是将 9 至 0 十个元素依次添加到 ivec 中。 Combining Dereference and Increment in a Single Expression在单个表达式中组合使用解引用和自增操作The following program, which prints the contents of ivec, represents a very common C++ programming pattern: 下面的程序使用了一种非常通用的 C++ 编程模式输出 ivec 的内容: vector<int>::iterator iter = ivec.begin(); // prints 10 9 8 ... 1 while (iter != ivec.end()) cout << *iter++ << endl; // iterator postfix increment
The precedence of postfix increment is higher than that of the dereference operator, so *iter++ is equivalent to *(iter++). The subexpression iter++ increments iter and yields a copy of the previous value of iter as its result. Accordingly, the operand of * is a copy of the unincremented value of iter. 由于后自增操作的优先级高于解引用操作,因此 *iter++ 等效于 *(iter++)。子表达式 iter++ 使 iter 加 1,然后返回 iter 原值的副本作为该表达式的结果。因此,解引用操作 * 的操作数是 iter 未加 1 前的副本。 This usage relies on the fact that postfix increment returns a copy of its original, unincremented operand. If it returned the incremented value, we'd dereference the incremented value, with disastrous results: The first element of ivec would not get written. Worse, we'd attempt to dereference one too many elements! 这种用法的根据在于后自增操作返回其操作数原值(没有加 1)的副本。如果返回的是加 1 后的值,则解引用该值将导致错误的结果:ivec 的第一个元素没有输出,并企图对一个多余的元素进行解引用。 |