3.4 introduction to the iterator
An iterator is a data type that checks elements in a container and traverses elements.
Each container defines its own iterator type, such:
Vector <int>: iteror ITER; // defines a variable named ITER. Its data type is the iterator type defined by vector <int>.
Begin and end operations
The iterator returned by begin points to the first element.
Vector <int>: itertor iter = ivec. Begin (); // ITER is the first element of ivec. ivec [0]
The iterator returned by the end points to the next one of the end elements, which is usually referred to as "beyond the end iterator ".
// Use the iterator to set all elements in the vector <int> to zero vector <int> VEC (); For (vector <int >:: iterator iter = Vec. begin (); iter! = Vec. End (); ++ ITER) {* iter = 0 ;}
The const_iterator type can only read elements in the container and cannot be changed!
Only one reference pointing to the const object can be obtained by unreferencing the const_iterator type.
// Only for read operations, use the const_iterator type # include <iostream> # include <vector> # include <string> using namespace STD; int main () {vector <int> VEC (10, 1); For (vector <int>: const_iterator iter = Vec. begin (); iter! = Vec. End (); ++ ITER) {cout <* ITER;} return 0 ;}
Exercise 3.17
// Redo 3.13. Use the iterator instead of the subscript # include <iostream> # include <vector> using namespace STD; int main () {vector <int> ivec; int ival; cout <"input numbers (CTRL + Z to end):" <Endl; while (CIN> ival) ivec. push_back (ival); If (ivec. size () = 0) {cout <"no elements! "<Endl; Return-1 ;}cout <" Sume of each pair of Adjacent Elements in the vector: "<Endl; vector <int> :: size_type CNT = 0; For (vector <int>: iterator iter = ivec. begin (); iter! = Ivec. end (); iter = ITER + 2) {cout <* ITER + * (ITER + 1) <"; CNT ++; if (CNT % 6 = 0) cout <Endl;} If (ivec. size () % 2! = 0) cout <"the last element is not been summed" <", the value is" <* (ivec. end ()-1) <Endl; return 0 ;}
// Redo 3.14 # include <iostream> # include <vector> # include <string> using namespace STD; int main () {vector <string> VEC; string val; while (CIN> Val) Vec. push_back (VAL); If (VEC. empty () {cout <"no data! "<Endl; Return-1 ;}vector <int >:: size_type CNT = 0; For (vector <string >:: iterator iter = Vec. Begin (); iter! = Vec. End (); ITER ++) {for (vector <string >:: size_type Index = 0; index! = (* ITER ). size (); index ++) (* ITER) [Index] = toupper (* ITER) [Index]); CNT ++; cout <* ITER <""; if (CNT % 8 = 0) cout <Endl;} return 0 ;}
Exercise 3.18
# Include <iostream> # include <vector> # include <string> using namespace STD; int main () {vector <int> ivec; cout <"Enter 10 numbers, twice the original value: "<Endl; int val; while (CIN> Val) ivec. push_back (VAL); For (vector <int>: iterator iter = ivec. begin (); iter! = Ivec. End (); ITER ++) {* iter = (* ITER) * 2; cout <* ITER <"" ;}return 0 ;}
Exercise 3.20
The preceding questions use the vector <int >:: iterator and vector <string >:: iterator iterators.
To access the vector elements whose element types are int and string
Note: any operation that changes the length of a vector will invalidate the existing iter !! After push_back, you can no longer trust the value of the iterator pointing to the vector !!