1.vector Initialization method
1.1vector, List, deque, String initialization method
//four types of initialization methods//Initialization Method 1//1.1 of 1;vector<int> Vec1 (Ten,1); Vector<int> vec2{0,1,2};//Initialization of listsvector<int>VEC3; //2. Initialization Method 2//2.1 requires the same container type and element typeVEC3 = VEC2;//Copy Initialization//3. Initialization Method 3//3.1 with VEC2vector<int> VEC4 = {0,1,2,3,4 }; //4. Initialization Method 4//4.1 requires the same element typeVector <int> Vec5 (Vec2.crbegin (), Vec2.crend ());
1.2 Array initialization method, the concept of arrays inC does not support copying, but the container array supports assignment operations, as follows:
array<int0123456 7 8 9 };array<int> copy = digits;
2. Assignment operation
- The iterator passed to assign cannot point to the container that called assign because its old element is replaced
names. Assign (names. Cbegin (),names. Cend ()) //same container names
- Assignment-related operations result in iterators, references, and pointers that point to the inside of the left container. The swap operation will not cause the container's iterators, references, and pointers to fail, and the following program can compile and run correctly.
//verify that using swap does not change iterators, pointers, references, and assignment changesvector<int> vec1{5,6,7,8,9}; Vector<int> vec2{0,1,2,3,4,5,6};//Initialization of listsvector<int>VEC3; //1. Swap for assignment, Exchange VEC1 and VEC2Vec1.swap (VEC2); for(vector<int>::iterator it = Vec1.begin (); It!= Vec1.end (); ++it) {cout<< *it <<" "; } cout<<Endl; //2. Direct Assignment VEC3 =vec2VEC3 =vec2; for(vector<int>::iterator it = Vec3.begin (); It! = Vec3.end (); ++it) {cout<< *it <<" "; }
- Assign assignment of a container
//1.1 of 1;vector<int> Vec1 (Ten,1); Vector<int> vec2{0,1,2,3,4,5,6,7,8,9};//Initialization of listsvector<int> vec3{0,1,2}; //2. Initialize Method 2 and assign Values//2.1 requires the same container type and element type//vec3 = vec2;//Copy InitializationSwap (VEC3, VEC2);//vec2{0,1,2};vec3{0,1,2,3,4,5,6,7,8}//3. Initialization Method 3//3.1 with VEC2vector<int> VEC4 = {0,1,2,3,4 }; //4. Initialization Method 4//4.1 requires the same element typeVector <int> Vec5 (Vec2.crbegin (), Vec2.crend ());//vec5{2,1,0}//5. Initialization method and assignment//Swap functionvector<int>vec6; Vec6.swap (VEC5);//vec5{};vec6{2,1,0}//6. Initialization method and assignment//Assignvector<int>Vec7; //6.1 Two iterator interval assignment {2,3,4,5,6,7}Vec7.assign (Vec3.cbegin () +2, Vec3.cend ()-2); //replacing an element with an element in the initialization list I1vector<int>Vec8; Vec8.assign (Ten,2);
3. Relational operators
- Containers of the same type
- Save elements of the same type
- The element type also defines the corresponding comparison operator
Container initialization, assignment, relational operations