C + + self-increment and decrement operators

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.