The self-increment and decrement operators in C + + provide a lot of convenience for adding 1 or minus 1 to objects, but they have both pre-and post-set forms and are prone to errors when used in combination with other operations, so write down their own understanding at a time.
The predecessor increment operator, which adds 1 to its operand, and the result of the operation is the modified number.
The pre-decrement operator, which causes its operand to be reduced by 1, the return result of the operation is the modified number,
inti =0, j =0; J= ++i; cout<<"i="<<i<<endl;//1cout<<"j="<<j<<endl;//1j = i++; cout<<"i="<<i<<endl;//2cout<<"j="<<j<<endl;//1
after the increment operator, which adds 1 to its operand, and the return result of the operation is the number before the modification.
The post-decrement operator, which causes its operand to be reduced by 1, the return result of the operation is the number before the modification,
inti =2, j =2; J= --i; cout<<"i="<<i<<endl;//1cout<<"j="<<j<<endl;//1j = i--; cout<<"i="<<i<<endl;//0cout<<"j="<<j<<endl;//1
recommendation : Use the post operator only if necessary! The reason is that the use of the predecessor operator to do less work, only need to add 1 after the return of 1 after the result of the row, the latter operator needs to save the original value of the operand, in order to return the operand before 1 as the result of the operation.
Combining dereference and self-increment operations in expressions
vector<int> ivec; int Ten ; while (cnt>0) ivec.push_back (CNT-); // 10,9,8...1 vector<int>::iterator iter = ivec.begin (); while (iter! =ivec.end ()) cout<<*iter++<<endl; // 10,9,8...1
Since the precedence of the post-increment operator is higher than the dereference, *iter++ is equivalent to * (iter++). However, if you change to *++iter, an error occurs because the Ivec first element has no output and attempts to dereference an extra element.
For a classic example:
*point1++ = *point2++;
Equivalent to:
*point1 = *Point2; + +point1; ++point2;
C + + self-increment and decrement operators