1. Each container type defines its own iterator type
Vector <int>: iterator ITER;
2. Begin () returns the iterator pointing to the first element.
End () returns the iterator pointing to the last and next element.
3. Use * ITER to unreference the iterator
4. The const_iterator type can be changed by itself. However, the value of the object to which it points cannot be changed.
5. // an iterator that cannot write Elements
Vector <int>: const_iterator ITER;
// An iterator whose value cannot change
Const vector <int> iterator ITER;
6.
Code:
-
- # Include <iostream>
-
- # Include <vector>
- Using NamespaceSTD;
-
- IntMain ()
-
- {
-
- IntReadin = 0;
-
- Vector <Int> Ivec;
-
- Cout <"Input numbers :"<Endl;
-
- While(CIN> readin)
- {
-
- Ivec. push_back (readin );
-
- }
-
- If(Ivec. Size () = 0)
-
- {
-
- Cout <"No number Iput! "<Endl;
-
- Return0;
-
- }
- Vector <Int>:: Iterator iter = ivec. Begin ();
-
- For(; ITER <ivec. End ()-1; iter = ITER + 2)
-
- {
-
- Cout <* ITER + * (ITER + 1) <"";
-
- }
-
- Cout <Endl;
-
- If(ITER! = Ivec. End ())
- {
-
- Cout <"A number was not calculated :"<* (Ivec. End ()-1 );
-
- }
-
- }
7. Arithmetic Operations of the iterator:
ITER + N; ITER-N;
Iter1-iter2; // calculate the distance between two iterators, which is the signed type of different_type
Any operation to change the length of a vector will invalidate the iterator.