Home >Backend Development >C#.Net Tutorial >Summary of common containers in C++_STL: associative containers and sequential containers in pairs
In this part, we first introduce the common operations of associated containers and sequential containers in a pair group pair, and then introduce the commonly used ones. Several specific associated containers.
Associative container is a type of storage object collection that supports efficient query by key. Most of the behavior of associative containers is the same as that of sequential containers. The essential difference between the two is that associative containers store and read elements by key, while sequential containers store and access elements sequentially by their position in the container.
Each element of the associated container contains a key and a real value. When an element is inserted into an associative container, the container's internal data structure places the element in the appropriate location according to certain rules based on the size of its key value. Associative containers have no so-called head and tail, only the largest element and the smallest element. Associative containers do not provide front, push_front, pop_front, back, push_back, and pop_back operations.
Standard STL associative containers are divided into two categories: set (set) and map (mapping), as well as their derivatives multiset (multi-key set) and multimap (multi-key mapping table). The underlying mechanisms of these containers are all implemented in RB-tree (red-black tree).
Associative containers share most of the operations of sequential containers, but Not all, associative containers can use the following three constructors:
C<T> c;
1
Create an empty container named c. C is the container type name, such as vector, and T is the element type, such as int or string. Works with all containers.
C<T> c(c2);
1
Create a copy c of container c2; c and c2 must have the same container type and store the same type of elements. Works with all containers.
C<T> c(b, e);
1
Create c whose elements are copies of the elements in the range identified by iterators b and e. Works with all containers.
Associative containers cannot be defined by the size of the container, because then the value corresponding to the key cannot be known What is it.
Associative containers support some relational operators of sequential containers.
Associated containers support begin, end, rbegin, and rend operations.
Associative containers support swap and assignment operations, but do not provide the assign function.
Associated containers support clear and erase functions, but the erase operation of associated containers returns the void type.
Associated containers support the size(), max_size(), and empty() functions as container size operations, but do not support the resize() function.
The pair pair contains two data values. The specific usage is as follows:
pair<T1, T2> p1;
1
Create an empty pair object whose two elements are of type T1 and T2 respectively. , initialized with value.
pair<T1, T2> p1(v1, v2);
1
Create a pair object, its two elements are of type T1 and T2 respectively, where the first member is initialized to v1, The second member is initialized to v2.
make_pair(v1, v2);
1
Create a new pair object with v1 and v2 values, and the types of its elements are the types of v1 and v2 respectively.
p1 < p2;
1
The less than operation between two pair objects, its definition follows dictionary order: if p1.first9b0d6015b2938b67bad9d94b40e0e780 Object name ;
If you use multiple identical pair objects, you can also use typedef to simplify their declaration:
typedef pair <string, string> Author; Author 对象名(初始值1,初始值2);
1
2
For the pair class, you can directly access its data members. Its members are all public and are named first member and second member respectively. They can be accessed using the dot operator.
Related articles:
c The use of Iterator in STL standard container
c Summary of review points z-12——STL string
Related videos:
The above is the detailed content of Summary of common containers in C++_STL: associative containers and sequential containers in pairs. For more information, please follow other related articles on the PHP Chinese website!