C ++ iterator, const_iterator and const iterator (7)

Source: Internet
Author: User

Vector is only one type in the container.
All the standard library containers define the corresponding iterator type. The iterator applies to all containers. Modern C ++ programs prefer to use the iterator instead of subscript operations to access container elements.

1.Iterator, const_iterator: traverses elements in the container and accesses the values of these elements. Iterator can change the element value, but const_iterator cannot. The pointer to C is a bit like
(Containers can + ITER, while vector can also ITER-N, ITER + n, n is an integer, iter1-iter2: The result is difference_type, the distance between the two elements of the table .)

2.The const_iterator object can be used for const vector or non-const vector. its own value can be changed (it can point to other elements), but it cannot rewrite the element value it points.

3.Const iterator is different from const_iterator: It must be initialized when a const iterator is declared. Once initialized, it cannot be changed. Once initialized, it can only be used

Instead of directing it to other elements. (Therefore, const iterator is useless)

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

 

For example, read a piece of text into a vector object. Each word is stored as an element in the vector. Converts each word in a vector object to a lowercase letter. Output the converted elements in a vector object. Each eight words are output in one row.

-- From C ++ primer 3.14
// A self-written code passed the test in vs2008. Because vc6.0 does not support the Standard C ++, You need to modify the preprocessing to test the code.
// Subscripts
# 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: The return type of SVEC [ix]. Size () 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;

}
// Use iterator to improve...
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)// Apply iterator for rewriting
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 it is not recommended (* ITER) [Index] = tolower (* ITER) [Index]);
Int IX = 0;
For (vector <string >:: const_iterator iter = SVEC. Begin (); iter! = SVEC. End (); ++ ITER, ++ IX)// Const_iterator is available for reading only
{
Cout <* ITER <'';
If (IX + 1) % 8 = 0)
{
Cout <Endl;
}
}
Getchar ();
Return 0;
}

 

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.