Difference between iterator and const_iterator and const iterator

Source: Internet
Author: User

If you pass over a type of const container, you can only use const_iterator to traverse it.

void Method (const vector<int> VINT)
{
Vector<int>::const_iterator ITER;
}

Simple example

Vector<int> Ivec;
Vector<int>::const_iterator citer1 = Ivec.begin ();
Const Vector<int>::iterator Citer2 = Ivec.begin ();
*citer1 = 1; Error
*citer2 = 1; Right
++citer1; Right
++citer2; Error

iterator and Const_iterator

All standard library containers define the appropriate iterator types. Iterators are applicable to all containers, and modern C + + programs tend to use iterators rather than subscript operations to access container elements.

1.iterator,const_iterator function: Iterates through the elements within the container and accesses the values of these elements. Iterator can change the element value, but const_iterator cannot be changed. It's a little bit like the C pointer.
(The container can be ++iter, and the vector can be iter-n, Iter+n,n is an integer, Iter1-iter2: The result is the Difference_type type, the distance between the two elements of the table.)

2.the Const_iterator object can be used with a const vector or a non-const vector, its own value can be changed (can point to other elements), but it cannot overwrite the value of the element it points to.

3.Const iterator is not the same as const_iterator: when declaring a const iterator, it must be initialized. Once initialized, it cannot change its value, and once it is initialized, it can only be used to

Change the element that it refers to, and you cannot point it to another element. (so the const iterator hardly uses)

Example Vector<int> nums (10); Nums is Nonconst
const Vector<int>::iterator CIT = Nums.begin ();
*cit = 1; Ok:cit can change its underlying element
++cit; Error:can ' t change the value of CIT

Example: read a piece of text into a vector object, each word stored as an element in the vector. Converts each word in a vector object to a lowercase letter. Outputs the converted elements in the vector object, one line for each eight words

--Excerpt from C++primer 3.14

Write your own code, in the VS2008 test pass, because it is VC6.0 to standard C + + support is not good, to test, you need to change the preprocessing.
Use subscript operation
#include <iostream>
#include <string>
#include <vector>
Using Std::vector;
Using Std::string;
Using Std::cout;
Using Std::cin;
Using Std::endl;
int main ()
{
Vector<string> Svec;
string Word;
while (Cin>>word)
Svec.push_back (word);
For (Vector<string>::size_type ix=0;ix!=svec.size (); ++ix)
{
For (String::size_type index=0;index!=svec[ix].size (); ++index)//Note: svec[ix].size () return type is String::size_type
{
Svec[ix][index]=tolower (Svec[ix][index]);
}
}
For (Vector<string>::size_type ix=0;ix!=svec.size (); ++ix)
{
cout<<svec[ix]<< ";
if ((ix+1)%8==0)
{
cout<<endl;
}
}
GetChar ();
return 0;
}
Improve with iterator ...
int main ()
{
Vector<string> Svec;
string Word;
while (Cin>>word)
Svec.push_back (word);
For (Vector<string>::iterator Iter=svec.begin (); Iter!=svec.end (); ++iter)//due to rewriting, the application iterator
For (String::iterator iter2= (*iter). Begin (); iter2!= (*iter). end (); ++iter2)
*iter2=tolower (*ITER2);
The above two lines of code are also changed to: for (String::size_type index=0;index!= (*iter). Size (); ++index)
But not recommended (*iter) [Index]=tolower ((*iter) [index]);
int ix=0;
For (Vector<string>::const_iterator Iter=svec.begin (); Iter!=svec.end (); ++iter,++ix)//Because only read, it can be used Const_ Iterator
{
cout<<*iter<< ";
if ((ix+1)%8==0)
{
cout<<endl;
}
}
GetChar ();
return 0;
}

const_iterator and Const iterator

Const_iterator:
C + + defines a type named const_iterator for each container type that can only be used to read elements within a container, but cannot change its value.
A reference to a const object is obtained by dereferencing the const_iterator type.
for (Vector<string>::const_iterator iter = Text.begin (); ITER! = Text.end (); + iter) {
cout << *iter << Endl; Ok:print each element in text
*iter = ""; Error: *iter is const
}

Const iterator:
The iterator of Const is not the same as the former, which means that iterator itself is a const rather than an object that the iterator points to.
Because iterator itself is a const, it must be initialized at the time of declaration, and it is not allowed to change its value after initialization (no more points to other elements).
Note: This const iterator is basically useless, because once initialized, it can only change the only point he points to, cannot point to other elements, strongly deprecated.
     vector<int> nums (Ten); //Nums is nonconst
     const Vector<int>::iterator cit = Nums.begin ();
     *cit = 1;               //Ok:cit can change its underlying element
     ++cit;    & nbsp;            //Error:can ' t change the value of CIT

Const_iterator can be used with const or non-const containers (because the value of an object cannot be modified), but const iterator can only be used for non-const containers (only values that are unique to the value can be modified).
Const Vector<int> Nines (10, 9); Cannot change elements in Nines
Error:cit2 could change the element it refers to and Nines are const
Const Vector<int>::iterator CIT2 = Nines.begin ();
Ok:it can ' t change an element value, so it can be used with a const vector<int>
Vector<int>::const_iterator it = Nines.begin ();
*it = 10; Error: *it is const
++it; Ok:it isn ' t const so we can change its value

Here are the detailed explanations I found on the Internet:
Const vector <int> VEC (10,10);

In the above statement, your VEC is defined as a constant container object! Note that the container is a constant container object, not a constant object!
(for example, not vector <const int>! The following code is easier to understand:

typedef vector <int> _vector;
Const _vector VEC (10,10);///const object, but the element in it is an int instead of a constant int!
)

But

Const vector <int>:: Iterator iter = Vec.begin ();

In the above statement, it is important to note that ITER is a constant iterator, but the data object it points to is an int instead of a const int, where the iterator has the ability to modify the VEC member, which is not allowed by the language feature (the golden rule about constant objects is that any modification of a constant object is possible, Are not allowed in the language)! Therefore, the above statement produces a compile-time error.

Const vector <int> VEC (10,10);
Vector <int>:: Iterator iter = Vec.begin ();

In the same way as the previous pair of statements, the only difference is that the former ITER is a constant iterator to the variable, and the latter is a variable-value iterator (the very type) that points to the variable.

So, in the following statement:

Vector <int> VEC (10,10);
Const vector <int>:: Iterator iter = Vec.begin ();

Good!vec is not a regular object, and of course there can be a ITER iterator that modifies its members!

Difference between iterator and const_iterator and const iterator

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.