Difference between CONST iterator and CONST_ITERATOR in C ++

Source: Internet
Author: User

The two are very different, but they are not very easy to understand, and they feel that the explanations are also various detours. Let's talk about my own understanding.
The const iterator, as its name implies, is a constant that cannot be changed. Its nature is determined by const. For example, we define a const iterator.
Vector <int> vv (10, 9 );
Const vector <int >:: iterator iter = vv. begin ();
It is incorrect when such a statement appears in the program.
++ Iter;
The reason is that iter is a constant, so it cannot be changed. In other words, iter can only point to one element of vv and cannot point to other elements.
However, this statement is correct:
× Iter = 10;
Although iter cannot point to other elements, the value of the first element to which it points can be changed.
For const_iterator, the opposite is true. For example, we define
Vector <int> vv (10, 9 );
Vector <int>: const_iterator iter;
A const_iterator iterator is defined. This iterator can be added by itself, but the elements it points to cannot be changed. For example
For (iter = vv. begin (); iter! = Vv. end (); ++ iter ){
Cout <* iter <endl;
}
This is correct, that is, the iter value can be changed. However
For (iter = vv. begin (); iter! = Vv. end (); ++ iter ){
* Iter = 0;
}
This is wrong, because the const_iterator iterator cannot change the value of the element to which it points.

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.