C ++ associated containers and bucket instances
C ++ related containers and bucket instances
11.2 Association container overview 11.2.2 requirements for keyword types
For ordered containers (map, multimap, set, and multiset), the keyword type must define the method for element comparison. By default, the <operator is used for comparison. When using a custom <operator, strict weak ordering must be defined as follows:
The two keywords cannot be less than or equal to each other. If k1 is less than or equal to k2 and k2 is less than or equal to k3, k1 must be less than or equal to k3. If neither of the two keywords is equal to or not, the two keywords are equivalent. When setting the comparison type (the second parameter of multiset), it should be a function pointer type.
11.2.3 pair type
11.3 associate container operations
11.3.1 associate container iterator
The map key is const, And the set key is const. Generally, generic algorithms are not used for associated containers. If necessary, they are used as the source sequence (copy to a sequence) or as a destination location (call inserter to bind the inserter ).
11.3.2 add element
Std: map
M; // four insert values. M. insert ({"a", 1}); m. insert (make_pair ("B", 22); m. insert (pair
("C", 333); m. insert (map
: Value_type ("d", 4444 ));
The values returned by insert and emplace are a pair, the frist member is the iterator (pointing to the element with the given keyword), and the second is bool, indicating whether the element is successfully inserted (not inserted in the container, returns false ). 11.3.3 delete an element
The returned value of erase is the number of elements to be deleted. That is, for containers with repeated keywords, the returned value may be greater than 1.
11.3.4 subscript operation for map
11.3.5 access element
When multiple elements in multimap and multiset share the same keyword, these elements are stored adjacent to each other.
// Three methods for obtaining the same element: multimap
M {1, "a" },{ 2, "B" },{ 1, "c" },{ 1, "d" }}; // Method 1: use find and countint count = m. count (1); // The number of search keywords is 1 (3 ). Auto it = m. find (1); // find the iterator whose first keyword is 1. For (size_t I = 0; I <count; ++ I, ++ it) cout <it-> second <endl; // output all values with the same keywords: a c d // Method 2: Use lower_bound (returns the first iterator not smaller than the given Key) and upper_bound (returns the first iterator greater than the given Key) for (auto beg = m. lower_bound (1), end = m. upper_bound (1); beg! = End; ++ beg) cout <beg-> second <endl; // method 3: Use euqal_range to obtain a pair, first is equivalent to lower_bound, and second is equivalent to upper_bound. For (auto pos = m. 1__range (1); pos. first! = Pos. second; ++ pos. first) cout <pos. first-> second <endl;
11.4 unordered container
Unordered containers are organized into a group of buckets. Each bucket stores 0 or more elements and maps the elements to the bucket using a hash function. Elements with the same hash value are stored in the same bucket. Calculating the hash value of an element and searching in a bucket are usually quick operations. You cannot directly define an unordered container whose keyword type is a custom type. To use a hash template, you must also provide the custom hash template version (instead of the = Operator (function pointer) and the hash value calculation function (function pointer )). <