STL-container (vector) begin () and rbegin (), end () and rend ()

1. Vector iterator first and end addresses begin () and end ()

You can use begin () and end () to obtain the address where the iterator can be used as a parameter in the code, as in the following code:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int test[] = { 111,222,333,444,555,666 };
 9     vector<int>num(test, test + 6);
10 
11     vector<int>::iterator it = num.begin();
12 
13     while (it != num.end())
14     {
15         cout << *it++ << endl;
16     }
17 
18     return 0;
19 }

Print the result:

 

 

2. Vector iterator reverses the first address and the tail address rbegin () and rend ()

If rbegin () and rend () are used, the output will be reversed, as shown in the following code:

1 #include <iostream>
 2 #include <vector>
 3  
4  using  namespace std;
 5  
6  int main ()
 7  {
 8      int test [] = { 111 , 222 , 333 , 444 , 555 , 666 };
 9      vector < int > num (test, test + 6 );
 10  
11      vector < int > :: reverse_iterator rIt = num.rbegin ();      // Note that here you need to use rIt of type reverse_iterator to accept the return value, iteriteior cannot be used
12 
13     while (rIt != num.rend())
14     {
15         cout << *rIt++ << endl;
16     }
17 
18     return 0;
19 }

Print the result:

 

 

 

 

 

 

 

 

=========================================================================================================================

Guess you like

Origin www.cnblogs.com/CooCoChoco/p/12749428.html