STL——Various functional methods of container

1. Whether the vector container is empty:

Use the vectorName.empty () method to obtain whether the container is empty, and return true if it is empty, otherwise return false, as follows:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int>num;
 9 
10     if (num.empty())
11     {
12         cout << "容器 num 为空" << endl;
13     }
14 
15     return 0;
16 }

Print the result:

 

 

 

 

2. Resize the vector:

Shrink: use vectorName.resize () to reallocate the size of the vector. The number of elements in the vector will change after the redistribution, but the size of the container will not change.

1 #include <iostream>
 2 #include <vector>
 3  
4  using  namespace std;
 5  
6  int main ()
 7  {
 8      vector < int > num ( 5 , 888 );
 9  
10      printf ( " Before using resize \ n " ) ;
 11      cout << " Number of num elements before using resize: " << num.size () << endl;
 12      cout << " Size of num space before using resize: " << num.capacity () << endl;
13      
14      for ( int i = 0 ; i <num.size (); i ++ )
 15      {
 16          cout << num [i] << endl;
 17      }
 18  
19      num.resize ( 2 );
 20  
21      cout << " The number of num elements after using resize: " << num.size () << endl;
 22      cout << " The space size of num after using resize: " << num.capacity () << endl;
 23      for ( int i = 0 ; i <num.size ();i++)
24     {
25         cout << num[i] << endl;
26     }
27 
28     return 0;
29 }

Print the result:

 

 Increase: The above is to reduce the capacity of the vector. Another use of resize () can expand the vector, as follows:

1 #include <iostream>
 2 #include <vector>
 3  
4  using  namespace std;
 5  
6  int main ()
 7  {
 8      vector < int > num ( 5 , 888 );
 9  
10      printf ( " Before using resize \ n " ) ;
 11      cout << " Number of num elements before using resize: " << num.size () << endl;
 12      cout << " Size of num space before using resize: " << num.capacity () << endl;
13      
14      for ( int i = 0 ; i <num.size (); i ++ )
 15      {
 16          cout << num [i] << endl;
 17      }
 18  
19      num.resize ( 10 , 111 );     // You can change Vector expansion, change 5 to 10, and initialize the newly expanded element to 111. If you do not write 111, the newly added element will be initialized to 0 
20  
21      cout << " The number of num elements after using resize: " << num .size () << endl;
 22      cout << " The space size of num after using resize: " << num.capacity () << endl;
23     for (int i = 0; i < num.size(); i++)
24     {
25         cout << num[i] << endl;
26     }
27 
28     return 0;
29 }

 

Print the result:

 

3. Get & modify the value of the first and last element of the vector container:

Get: Use vectorName.front () and vectorName.back () to get the reference of the first element and the last element of vector, as follows:

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 , 777 , 888 , 999 };
 9      vector < int > num (test, test + 9 );
 10  
11      cout << " The first element of num is: "<< num.front () << endl;
 12      cout << " The first element of num is: " << num.back () << endl;
 13  
14      return  0 ;
 15 }

Print the result:

 

 Modification: Because the reference is returned, then we can also perform assignment operations, as 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 , 777 , 888 , 999 };
 9      vector < int > num (test, test + 9 );
 10  
11      cout << " The first element of num is: "<< num.front () << endl;
 12      cout << " The first element of num is: " << num.back () << endl;
 13  
14      cout << " ======== = Change the value of the first and the last ======== " << endl;
 15      num.front () = 1 ;
 16      num.back () = 9 ;
 17  
18      for ( int i = 0 ; i <num.size (); i ++ )
 19      {
 20          cout << num.at (i) << endl;
21     }
22 
23     return 0;
24 }

Print the result:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

Guess you like

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