1. Associative containers and sequential containers
There are two types of containers in C + +: Sequential containers and associative containers, and sequential containers include: vectors, lists, deque, etc. The associated containers are mainly map and set. Such as:
1, Vector Basic use
#include <iostream>#include<stdlib.h>#include<string.h>#include<algorithm>#include<vector>using namespacestd;//using templates for outputTemplate <typename t>voidPrint (vector<t>a) { //Output Method 1 for(Auto i:a) {cout<<i<<'\ t'; } cout<<Endl; //Output Method 2TypeName Vector<t>::iterator it;//the front to add TypeName, to make no mistakes for(It=a.begin (); It!=a.end (); it++) {cout<<*it<<'\ t'; } cout<<Endl; //Output Method 3, here just to say this can also outputcout<<a[0]<<'\ t'<<a[1]<<'\ t'<<a[2]<<'\ t'<<a[3]<<'\ t'<<a[4]<<Endl; cout<<"********************"<<Endl;}intMain () {/*Vector Initialization Object*/Vector<int> v1;//v1 is an empty vector, the potential element is the int type, when executed, the default initializationv1={1,2,3,4,5}; Print (v1); Vector<int> v2 (v1);//equivalent to vector<int> v2=v1Print (v2); Vector<Char> V3 (5,'a');//v3 contains 5 a charactersprint (v3); Vector<string> v4{"AA","SS","DD","FF","GG"};//v4 Assignment Valueprint (v4); Vector<float> V5 (5);//v5 of 5 0.0v5[0]=1.1;//v5[6]=8.8; vectors can be accessed using subscript, but cannot use subscript to add elementsprint (v5);/*-------------------------------------------------------------------------------*/ /*Vector Operation*/Vector<string>V6; if(V6.empty ())//whether the vector is empty{cout<<"------"<<"V6 is empty"<<"-------"<<Endl; } strings="qwe"; for(intI=0;i<5; i++) {v6.push_back (s);//add an element at the end of the} v6.pop_back ();//End Deletes aV6.push_back ("1234"); Print (V6); cout<<"------"<<"the length of the V6 is:"<<v6.size () <<"------"<<Endl; V6=V4;//Copy the elements in the V4 assignment to V6 if(v6==v4) {cout<<"------"<<"v6==v4"<<"------"<<Endl; }/*-------------------------------------------------------------------------------*/ /*Vector Common Operations*/Vector<int> array={1,6,2,6,3,6,4,6,5,6}; Array.erase (Remove (Array.begin (), Array.end (),6), Array.end ());//need to add a header file algorithm /*The remove function uses: *remove (start address, end address, element to be removed), returns the address of the first number of numbers to be replaced, such as the vector * Primitive array: [1,6,2,6,3,6,4,6,5,6], after using the Remove function [ 1,2,3,4,5,6,4,6,5,6], * Return address is at position 5 (0 start) of the address of 6, the following output number 5. cout<<* (Remove (Array.begin (), Array.end (), 6)-1) <<endl;*/ /*another way to remove 6 vector<int>::iterator it1; It1=array.begin (); For (It1=array.begin (); It1!=array.end (); it1++) {if (6==*it1) {array.erase (IT1);//delete it1, IT1 will refer to The next number, that is, 6, and then the self-add will point to a 6, that is, the adjacent 6 is deleted it1--;//Plus this sentence will not be wrong}}*/print (array); Vector< vector<int> > INTVV;//Vector implements two-dimensional arraysvector<int>Intv; inti,j; for(i=0;i<3;++i) {intv.clear (); for(j=0;j<5;++j) Intv.push_back (i*Ten+j); Intvv.push_back (INTV); } for(i=0;i<3;++i) { for(j=0;j<5;++j) cout<<intVV[i][j]<<'\ t'; cout<<Endl; } return 0;}
View Code
Use of C + + STL basic containers