Increment/decrement operators
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
递增/递减运算符递增或递减的对象的值.
原文:
Increment/decrement operators increments or decrements the value of the object.
Operator name | Syntax | Overloadable | Prototype examples (for class T) | |
---|---|---|---|---|
Inside class definition | Outside class definition | |||
pre-increment | ++a
|
Yes | T& T::operator++(); | T& operator++(T& a); |
pre-decrement | --a
|
Yes | T& T::operator--(); | T& operator--(T& a); |
post-increment | a++
|
Yes | T T::operator++(int); | T operator++(T& a, int); |
post-decrement | a--
|
Yes | T T::operator--(int); | T operator--(T& a, int); |
|
目录 |
[编辑] 解释
“前增加”和“预减”运营商增加或减小该值的对象,并返回一个引用到的结果.....
原文:
pre-increment and pre-decrement operators increments or decrements the value of the object and returns a reference to the result.
“后增加”和“减”创建一个副本的对象,递增或递减的对象的值,并返回从之前的增量或减量的副本.
原文:
post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement.
[编辑] 内置的前缀运算符
对于每一个可以用volatile限定
A
以外bool,并为每个可以用volatile限定P
可以用CV合格的对象类型的指针,下面的函数签名参与重载解析:算术类型原文:
For every optionally volatile-qualified arithmetic type
A
other than bool, and for every optionally volatile-qualified pointer P
to optionally cv-qualified object type, the following function signatures participate in overload resolution: A& operator++(A&) |
||
bool& operator++(bool&) |
(已弃用) | |
P& operator++(P&) |
||
A& operator--(A&) |
||
P& operator--(P&) |
||
一个内置的前缀递增或递减运算的操作数必须是一个可修改的左值(非const引用)的非布尔值的算术类型或对象类型的指针完成。这些操作数,表达++x是完全等同于x+=1,--x,这是完全等同的表达x-=1,其结果是更新操作,返回的左值,所有的算术转换规则和指针运算规则定义算术运算符申请.
原文:
The operand of a built-in prefix increment or decrement operator must be a modifiable lvalue (non-const reference) of non-boolean arithmetic type or pointer to complete object type. For these operands, the expression ++x is exactly equivalent to x+=1, and the expression --x is exactly equivalent to x-=1, that is, the result is the updated operand, returned as lvalue, and all arithmetic conversion rules and pointer arithmetic rules defined for 算术运算符 apply.
如果操作数的前递增运算符类型bool,它被设置为true(已弃用).
原文:
If the operand of the preincrement operator is of type bool, it is set to true (已弃用).
[编辑] 内置的后缀运算符
对于每一个可以用volatile限定
A
以外bool,并为每个可以用volatile限定P
可以用CV合格的对象类型的指针,下面的函数签名参与重载解析:算术类型原文:
For every optionally volatile-qualified arithmetic type
A
other than bool, and for every optionally volatile-qualified pointer P
to optionally cv-qualified object type, the following function signatures participate in overload resolution: A operator++(A&, int) |
||
bool operator++(bool&, int) |
(已弃用) | |
P operator++(P&, int) |
||
A operator--(A&, int) |
||
P operator--(P&, int) |
||
一个内置的后缀递增或递减运算的操作数必须是一个可修改的左值(非const引用)的非布尔值的算术类型或对象类型的指针完成。其结果是一个prvalue,这是复制的原始值的操作数。作为一个副作用,该运营商修改其参数值
arg
如果通过评估arg += 1或arg -= 1,分别为递增和递减。所有的算术转换规则和指针运算规则定义算术运算符申请.原文:
The operand of a built-in postfix increment or decrement operator must be a modifiable lvalue (non-const reference) of non-boolean arithmetic type or pointer to complete object type. The result is a prvalue, which is a copy the original value of the operand. As a side-effect, this operator modifies the value of its argument
arg
as if by evaluating arg += 1 or arg -= 1, for increment and decrement respectively. All arithmetic conversion rules and pointer arithmetic rules defined for 算术运算符 apply.如果类型bool的后递增操作符的操作数,它被设置为true(已弃用).
原文:
If the operand of the postincrement operator is of type bool, it is set to true (已弃用).
[编辑] 示例
#include <iostream> int main() { int n = 1; int n2 = ++n; int n3 = ++ ++n; int n4 = n++; // int n5 = n++ ++; // compile error // int n5 = n + ++n; // undefined behavior std::cout << "n = " << n << '\n' << "n2 = " << n2 << '\n' << "n3 = " << n3 << '\n' << "n4 = " << n4 << '\n'; }
输出:
n = 5 n2 = 2 n3 = 4 n4 = 4
[编辑] 注释
由于副作用参与,内置的增量和减量运算,必须谨慎使用,以避免不确定的行为因违反排序规则.
原文:
Because of the side-effects involved, built-in increment and decrement operators must be used with care to avoid undefined behavior due to violations of 排序规则.
在操作过程中,因为临时对象的拷贝构造“前增加”或“预减”运营商在不使用的上下文中返回的值通常更有效.
原文:
Because a temporary copy of the object is constructed during the operation, pre-increment or pre-decrement operators are usually more efficient in contexts where the returned value is not used.
[编辑] 标准库
递增和递减运算符重载,许多标准库类型。特别是,每一个
Iterator
重载运算符+ +和每BidirectionalIterator
重载运算符 - ,即使这些运营商没有特定的迭代器操作.原文:
Increment and decrement operators are overloaded for many standard library types. In particular, every
Iterator
overloads operator++ and every BidirectionalIterator
overloads operator--, even if those operators are no-ops for the particular iterator.
| |
递增或递减一个原子值 原文: increments or decrements the atomic value by one (公共成员函数of std::atomic )
| |
递增或递减滴答计数 原文: increments or decrements the tick count (公共成员函数of std::chrono::duration )
| |
| |
进步的迭代器 (公共成员函数of std::raw_storage_iterator )
| |
进步的迭代器 (公共成员函数of std::reverse_iterator )
| |
减少了迭代器 (公共成员函数of std::reverse_iterator )
| |
no-op (公共成员函数of std::back_insert_iterator )
| |
no-op (公共成员函数of std::front_insert_iterator )
| |
no-op (公共成员函数of std::insert_iterator )
| |
进步的迭代器 (公共成员函数of std::move_iterator )
| |
减少了迭代器 (公共成员函数of std::move_iterator )
| |
推进istream_iterator (公共成员函数of std::istream_iterator )
| |
no-op (公共成员函数of std::ostream_iterator )
| |
推进istreambuf_iterator (公共成员函数of std::istreambuf_iterator )
| |
no-op (公共成员函数of std::ostreambuf_iterator )
| |
推进regex_iterator (公共成员函数of std::regex_iterator )
| |
推进regex_token_iterator (公共成员函数of std::regex_token_iterator )
|
[编辑] 另请参阅
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||