Pair group Pair
In this part, we introduce the common operation of the associative container and the sequential container in group pair, and then introduce several specific associative containers.
1. Related containers
An associative container is a type of storage object collection that supports efficient querying through keys. The most common behavior of an associative container is the same as the sequential container, where the correlation container stores and reads elements through keys, while sequential containers store and access elements in the order in which they are placed in the container.
Each element of the associative container contains a key value (key) and a real value. When an element is inserted into an associative container, the container's internal data structure is placed in the appropriate position in a particular rule, according to the size of its key value. Associative containers do not have the so-called Kinsoku, only the largest and smallest elements, and the associated containers do not provide front, Push_front, Pop_front, back, push_back, and pop_back operations.
The Standard STL Association container is divided into set (set) and map (map), as well as the two major classes of derivative multiset (multi-key set) and Multimap (Multi-key mapping table). The underlying mechanisms of these containers are completed with rb-tree (red-black tree).
2. Common operation of associative containers and sequential containers
1. Constructor for associative container and sequential container sharing
The associated container shares the operations of most sequential containers, but not all, and the associated container can use the following three constructors:
C<t> C;
Create an empty container named C. C is the name of the container type, such as Vector,t is an element type, such as int or string. Applies to all containers.
C<t> C (C2);
Create a copy of the container C2 c;c and C2 must have the same container type and hold elements of the same type. Applies to all containers.
C<t> C (b, E);
Create C, whose elements are copies of the elements in the range indicated by iterators B and E. Applies to all containers.
2. Associated containers and other functions shared by the sequential container
The associated container cannot be defined by the size of the container, because there is no way to know what the value of the key corresponds to.
Associative containers support some relational operators for sequential containers.
The associative container supports begin, end, Rbegin, rend operations.
The associative container supports swap and assignment operations, but does not provide a assign function.
The association container supports the clear and erase functions, but the erase operation of the associative container returns the void type.
The association container supports size (), max_size (), empty () functions as container-sized operations, but does not support the resize () function.
3. Operation for group pair type
A pair of pairs contains two data values. Here's how to use it:
Pair<t1, t2> p1;
Creates an empty pair object whose two elements are T1 and T2 types, with value initialization.
Pair<t1, t2> p1 (v1, v2);
Creates a pair object whose two elements are the T1 and T2 types, where the first member is initialized to the V1,second member and initialized to V2.
Make_pair (v1, v2);
Creates a new pair object with V1 and V2 values whose elements are of type V1 and V2, respectively.
P1 < P2;
A less than operation between two pair objects whose definition follows the dictionary order: if p1.first<p2.first
or !(p2.first<p1.first)&&p1.second<p2.second
, returns True.
P1 = = P2;
If the first and second members of the two pair object are equal in turn, the two objects are equal, and the operation uses the = = operator of its element.
P.first, P.second;
Returns a public data member named first, second in P.
4. Creation and initialization of pair
When creating a pair object, you must provide two type names, one for each of the two data members of the pair object, and the two type names can be different. If you do not provide display initialization when you create a pair object, the default constructor is called to initialize its members, or you can use () to display the initialization directly when you create the object:
-pair< Type 1, type 2> object name;
If you use multiple identical pair objects, you can also use typedef to simplify their declarations:
typedef Pair <string, string> Author; Author Object Name (initial value 1, initial value 2);
For the pair class, you can access its data members directly, whose members are public, named first members and second members, and accessed using the dot operator.
Related articles:
Iterator use of C + + STL standard Container
C + + Review Essentials summary Z of 12--stl string
Related videos:
C # Tutorials