Personal summary if there are errors, please correct them
What is a container? If you have learned the c language, it will be very simple. The container is like an array, a structure for storing data. The container also needs to indicate the data type to be stored when applying. You only need to insert or take out data through functions.
1, Near container
The concept of near container is very simple. Structures that can store data, such as array and string, are called near containers
2, Sequential container
vector
-
Vector is a vector type that can hold many types of data
-
Add header file #include before vector operation
-
The capacity expansion method is 1.5 times
Initialization mode -
vector a(10) ; // Define 10 shaping element vectors. There is no initial value, and the value is random;
-
vectora(10,1); // The same as above has an initial value of 1
-
vectora(b); // Copy the number in B to a, and call copy constructor.
-
vectora(b.begin(),b.end()); // Assign all elements in vector B to a, and the type of a is int
-
vectora(b,b+5); //(int b[8]={0,1,2,3,4,5,6,7}) assign the first five numbers in B to a. note that the types should be the same
-
vector a(L.begin(), L.end()); Initialize vector container with list container
#include<vector> vector<int> a,b; a.assign(b.begin(),b.begin()+3);//b is a vector, and 0-2 elements of b are assigned to vector a a.assign(4,2);//A contains four elements with a value of 2 a.back();//Returns the last element of a a.front();//Returns the first element of a a[i];//Returns the ith element of a if and only if a exists a.clear();//Empty elements in a a.empty();//Judge whether a is empty. If it is empty, it returns true. If it is not empty, it returns false a.pop_back();//Deletes the last element of the a vector a.erase(a.begin()+1,a.begin()+3);//Delete the first (from the 0) to the second element in a, that is, the deleted element starts from a.begin()+1 (including it) to a.begin()+3 (excluding it) a.push_back(5);//Insert an element after the last vector of a with a value of 5 a.insert(a.begin()+1,5);//Insert the value 5 at the position of the first element of a (calculated from the 0th), a.insert(a.begin()+1,3,5);//Insert 3 numbers at the position of the first element of a (calculated from the 0), and their values are all 5 a.insert(a.begin()+1,b+3,b+6);//b is an array. Insert the third element of b to the fifth element (excluding b+6) at the position of the first element of a (calculated from the 0th element) a.size();//Returns the number of elements in a a.capacity();//Returns the total number of elements a can hold in memory a.resize(10);//Adjust the number of existing elements of a to 10, delete more and supplement less, and its value is random a.resize(10,2);//Adjust the number of existing elements of a to 10, delete more and supplement less, and its value is 2 a.reserve(100);//Expand the capacity of a to 100, a.swap(b);//b is a vector, and the elements in a and b are exchanged as a whole a==b;//b is the vector, and the vector comparison operation is also! = > = > < =< a.max_size();//The amount of current data that can be stored in memory
list
list<int> L; for (int i = 0; i < 10; i++) { L.push_back(i);//Insert data into the list } show_con(L);//Print vector<int> v1(L.begin(),L.end());//Initialization mode show_con(v1); L.push_front(3);//Head insert L.pop_front();//Header deletion L.resize(20);//Forcibly change the container size. If there is not enough space to apply for new space, initialize with 0
deque
Bottom implementation of double ended queue: two-dimensional array
Insert the first one from the middle, the head to the front and the tail to the back
Capacity expansion: the back needs to be added, and the front needs to be added
priority_queue priority queue
The calling method is the same as that of vector
Container Adapters
stack: - depends on deque
queue: - dependent deque
It depends on the nature of stack and queue
Associated container
Set set
Automatic sorting data is added, deleted, modified and queried in order, with low time complexity and no data duplication is allowed
(own fast find algorithm - bottom red black tree)
set<int> s; s.insert(56);//insert s.insert(78); s.insert(34); s.insert(28); s.insert(97); s.insert(97); s.erase(97);//delete count(n);0 or 1 appears several times in s.find(99);find s Are there any 99
Multiset multiset (similar to set, data can be repeated)
count(n);Several times in
map (key value pair)
The mapping table data is ordered by key
Bottom implementation: red black tree
Duplicate: duplicate is not allowed
make_pair() is a function template and returns a class template object
It - > first key
It - > second value
Find: Map < int, string >:: iterator it = mm. Find (51); You can only find by pressing the key
map<int, string> mm; mm.insert(make_pair(1, "aaaa")); mm.insert(make_pair(3, "ccc")); mm.insert(make_pair(5, "eeee")); mm.insert(make_pair(2, "bbb")); mm.insert(pair<int, string>(4, "ddd")); map<int, string>::iterator it = mm.begin(); for (; it != mm.end(); it++) { cout << it->first << "--->"; cout << it->second <<endl; } map<int, string>::iterator it1 = mm.find(5); if (it1 != mm.end()) { cout << it1->first << "--->"; cout << it1->second << endl; } mm.empty();//Air judgment mm.clear();//eliminate //mm.swap(); mm.erase(5);//delete mm.count(5);How many are there mm.size();//Get size
multimap: multiple mapping table
Duplicate: duplicate key value pairs are allowed