Vector vector container (summary of commonly used methods)

As for the learning of vector container in STL, it's better to watch the code after compiling and running, and see the e...

As for the learning of vector container in STL, it's better to watch the code after compiling and running, and see the execution result at the same time. I still want to say that it's better to watch other people's code 100 times, rather than write it by yourself.

Vector vector container can not only access elements randomly like array, but also insert elements in the tail at any time. It is simple and efficient, and can completely replace array.

The biggest highlight of vector is that it has the function of memory automatic management, which can dynamically adjust the memory space occupied when inserting and deleting elements.

It is worth noting that two important methods in the vector container, begin() returns the iterator of the first element position, and end() returns the iterator of the next element position of the last element.

1 //about STL in vector It is better to read the code after compiling and running the container and see the execution result at the same time, but it's better to read other people's code a hundred times than to write it by yourself 2 #include <vector>//Header file 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 void print(vector <int> v); 8 bool mycmpare(const int &a, const int &b){ 9 return a>b; 10 } 11 int main () 12 { 13 //establish vector There are three common ways to store objects. The storage element type here is int,It could be double,char,long long Basic data types, even string Basic character sequence container 14 vector <int> v1;//Definition of the number of elements without specifying a container a vector container for storing integers 15 cout<<"v1: "<<endl; 16 print(v1); 17 /*Operation results 18 v1: 19 Size: 0 20 */ 21 22 vector <int> v2(5);//Defines the number of elements in the specified container. A vector container with a size of 10 is used to store integers. It is initialized to 0 by default 23 cout<<"v2: "<<endl; 24 print(v2); 25 /*Operation results 26 v2: 27 Size: 5 28 0 0 0 0 0 29 */ 30 31 vector <int> v3(5,1);//You can also specify an initial value, which is 1 here 32 cout<<"v3: "<<endl; 33 print(v3); 34 /*Operation results 35 v3: 36 Size: 5 37 1 1 1 1 1 38 */ 39 40 //In addition, it doesn't matter whether you specify the size in advance. If you specify the size, you can use it at any time push_back()Yes vector Container tail expansion 41 v1.push_back(1);//Emptiness oriented vector Container tail expansion, additional element is 1 42 cout<<"v1: "<<endl; 43 print(v1); 44 v3.push_back(2);//To the vector Container tail expansion, additional element is 2 45 cout<<"v3: "<<endl; 46 print(v3); 47 /*Operation results 48 v1: 49 Size: 1 50 1 51 52 v3: 53 Size: 6 54 1 1 1 1 1 2 55 */ 56 57 //Insert element use insert()Method, requiring the insertion position to be the position of the iterator, not the subscript of the element 58 v3.insert(v3.begin(),3);//Insert 3 at the front 59 cout<<"v3: "<<endl; 60 print(v3); 61 62 v3.insert(v3.end(),3);//Add 3 at the end, which is equivalent to push_back() 63 cout<<"v3: "<<endl; 64 print(v3); 65 /*Operation results 66 v3: 67 Size: 7 68 3 1 1 1 1 1 2 69 70 v3: 71 Size: 8 72 3 1 1 1 1 1 2 3 73 */ 74 75 int i; 76 for(i=0;i < v3.size();i++){//Only assigned to expanded position 77 v3[i]=i; 78 } 79 //Use when you want to delete an element or all elements in an interval erase()method 80 v3.erase(v3.begin()+2);//Delete the second element, counting from 0 81 cout<<"v3: "<<endl; 82 print(v3); 83 /*Operation results 84 v3: 85 Size: 7 86 0 1 3 4 5 6 7 87 */ 88 v3.erase(v3.begin()+1,v3.begin()+3);//Delete all elements in the first to third element interval 89 cout<<"v3: "<<endl; 90 print(v3); 91 /*Operation results 92 v3: 93 Size: 5 94 0 4 5 6 7 95 */ 96 //It can be seen from the results, erase()The same method insert()Method, the position of the operation is only the position of the iterator, not the subscript of the element 97 98 //To empty vector(),use clear()Method delete once vector All elements in 99 cout<<"v2: "<<endl; 100 print(v2); 101 /*Operation results 102 v2: 103 Size: 5 104 0 0 0 0 0 105 */ 106 v2.clear(); 107 if(v2.empty()) cout<<"v2 After use clear()Empty after method\n"; 108 print(v2); 109 /*Operation results 110 v2 Empty after using the clear() method 111 Size: 0 112 */ 113 114 //To reverse an iterator interval element in a vector, use the reverse()Reverse permutation algorithm,Need to add algorithm Header file 115 cout<<"v3 Before reverse:"<<endl; 116 print(v3); 117 reverse(v3.begin(),v3.end());//Reverse all 118 cout<<"v3 After reverse arrangement:"<<endl; 119 print(v3); 120 /*Operation results 121 v3 Before reverse: 122 Size: 5 123 0 4 5 6 7 124 125 v3 After reverse arrangement: 126 Size: 5 127 7 6 5 4 0 128 */ 129 130 //To sort an iterator interval element in a vector, use the sort()algorithm 131 cout<<"v3 Before ascending:"<<endl; 132 print(v3); 133 sort(v3.begin(),v3.end());//Default ascending 134 cout<<"v3 After ascending:"<<endl; 135 print(v3); 136 /*Operation results 137 v3 Before ascending: 138 Size: 5 139 7 6 5 4 0 140 141 v3 After ascending: 142 Size: 5 143 0 4 5 6 7 144 */ 145 146 //Custom sort comparison function, descending here 147 cout<<"v3 Before descending:"<<endl; 148 print(v3); 149 sort(v3.begin(),v3.end(),mycmpare); 150 cout<<"v3 After descending:"<<endl; 151 print(v3); 152 /*Operation results 153 v3 Before descending: 154 Size: 5 155 0 4 5 6 7 156 157 v3 After descending: 158 Size: 5 159 7 6 5 4 0 160 */ 161 } 162 163 void print(vector <int> v) 164 { 165 //cout<<"Subscript access:"<<endl; 166 cout<<"The size is:"<<v.size()<<endl; 167 int i; 168 for(i=0;i< v.size();i++){ 169 cout<<v[i]<<' '; 170 } 171 cout<<endl<<endl; 172 173 /*cout<<"Access with iterator: "< endl; 174 //Define iterator variable it, with the type consistent with the container element type 175 vector<int>::iterator it; 176 for(it=v.begin(); it != v.end(); it++){ 177 cout<<*it<<' '; 178 } 179 cout<<endl<<endl;*/ 180 }

13 May 2020, 12:04 | Views: 5346

Add new comment

For adding a comment, please log in
or create account

0 comments