The difference between C + +------Const iterators and Const_iterator

Source: Internet
Author: User

A const iterator, as the name implies, is an iterator that cannot be changed, and is a constant whose nature is determined by the Const. Let's say we define a const iterator like this.

vector<int> VV (9);   const vector<int

is incorrect when the following statement appears in the program

++iter;  

The reason for this is that ITER is a constant and therefore cannot be changed. In other words, ITER can only point to the one element of the VV, and cannot point to other elements.

But such statements are correct:

ten;  

Because although ITER cannot point to other elements, the value of the first element it points to can be changed.

And for Const_iterator, the opposite is the case. For example, we define this.

vector<int> VV (9);  Vector<int>:: Const_iterator iter;  

A const_iterator iterator is defined. This iterator can be added on its own, but the element it points to cannot be changed. For example

 for (iter = Vv.begin (); ITER! = Vv.end (); +iter)      {<< *iter << endl;  }  

This is true, that is, the value of ITER itself can be changed. But

 for (iter = Vv.begin (); ITER! = Vv.end (); + iter)     {0  ;  }

This is not true because the Const_iterator iterator cannot change the value of the element it points to.

C + +------The difference between const iterators and const_iterator

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.