Increment/decrement operators
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
递增/递减运算符递增或递减的对象的值.
Original:
Increment/decrement operators increments or decrements the value of the object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
| 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); |
| ||||
[编辑] 解释
“前增加”和“预减”运营商增加或减小该值的对象,并返回一个引用到的结果.....
Original:
pre-increment and pre-decrement operators increments or decrements the value of the object and returns a reference to the result.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
“后增加”和“减”创建一个副本的对象,递增或递减的对象的值,并返回从之前的增量或减量的副本.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 内置的前缀运算符
对于每一个可以用volatile限定
A以外bool,并为每个可以用volatile限定P可以用CV合格的对象类型的指针,下面的函数签名参与重载解析:算术类型Original:
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:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
| A& operator++(A&) |
||
| bool& operator++(bool&) |
(过时了) | |
| P& operator++(P&) |
||
| A& operator--(A&) |
||
| P& operator--(P&) |
||
一个内置的前缀递增或递减运算的操作数必须是一个可修改的左值(非const引用)的非布尔值的算术类型或对象类型的指针完成。这些操作数,表达++x是完全等同于x+=1,--x,这是完全等同的表达x-=1,其结果是更新操作,返回的左值,所有的算术转换规则和指针运算规则定义算术运算符申请.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
如果操作数的前递增运算符类型bool,它被设置为true(过时了).
Original:
If the operand of the preincrement operator is of type bool, it is set to true (过时了).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 内置的后缀运算符
对于每一个可以用volatile限定
A以外bool,并为每个可以用volatile限定P可以用CV合格的对象类型的指针,下面的函数签名参与重载解析:算术类型Original:
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:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
| 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,分别为递增和递减。所有的算术转换规则和指针运算规则定义算术运算符申请.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
如果类型bool的后递增操作符的操作数,它被设置为true(过时了).
Original:
If the operand of the postincrement operator is of type bool, it is set to true (过时了).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
#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'; }
Output:
n = 5 n2 = 2 n3 = 4 n4 = 4
[编辑] 注释
由于副作用参与,内置的增量和减量运算,必须谨慎使用,以避免不确定的行为因违反排序规则.
Original:
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 排序规则.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
在操作过程中,因为临时对象的拷贝构造“前增加”或“预减”运营商在不使用的上下文中返回的值通常更有效.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 标准库
递增和递减运算符重载,许多标准库类型。特别是,每一个
Iterator重载运算符+ +和每BidirectionalIterator重载运算符 - ,即使这些运营商没有特定的迭代器操作.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Original: overloads for arithmetic types The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| 递增或递减一个原子值 Original: increments or decrements the atomic value by one The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::atomic)
| |
| 递增或递减滴答计数 Original: increments or decrements the tick count The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::chrono::duration)
| |
Original: overloads for iterator types The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| 进步的迭代器 Original: advances the iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::raw_storage_iterator)
| |
| 进步的迭代器 Original: advances the iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::reverse_iterator)
| |
| 减少了迭代器 Original: decrements the iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::reverse_iterator)
| |
| no-op (公共成员函数of std::back_insert_iterator)
| |
| no-op (公共成员函数of std::front_insert_iterator)
| |
| no-op (公共成员函数of std::insert_iterator)
| |
| 进步的迭代器 Original: advances the iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::move_iterator)
| |
| 减少了迭代器 Original: decrements the iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::move_iterator)
| |
| 推进istream_iterator Original: advances the istream_iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::istream_iterator)
| |
| no-op (公共成员函数of std::ostream_iterator)
| |
| 推进istreambuf_iterator Original: advances the istreambuf_iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::istreambuf_iterator)
| |
| no-op (公共成员函数of std::ostreambuf_iterator)
| |
| 推进regex_iterator Original: advances the regex_iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::regex_iterator)
| |
| 推进regex_token_iterator Original: advances the regex_token_iterator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数of std::regex_token_iterator)
| |
[编辑] 另请参阅
| Common operators | ||||||
|---|---|---|---|---|---|---|
| 分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
|
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
| Special operators | ||||||
|
static_cast将一种类型转换到另一个兼容的类型
Original: static_cast converts one type to another compatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. dynamic_cast将虚基类到派生class
Original: dynamic_cast converts virtual base class to derived class The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. const_cast转换类型兼容型,与不同的cvqualifiers
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. reinterpret_cast转换类型不兼容的type
Original: reinterpret_cast converts type to incompatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. new个分配memory
Original: new allocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. delete会释放memory
Original: delete deallocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof查询的大小的type
Original: sizeof queries the size of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof...查询的大小的参数组(C++11 起)
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. typeid查询的类型一个type
信息 Original: typeid queries the type information of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. noexcept进行检查,,如果的表达可以抛出一个异常(C++11 起)
, Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. alignof查询类型(C++11 起)对齐要求
Original: alignof queries alignment requirements of a type (C++11 起) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||