C++_STL-unordered_map,unordered_multimap,unordered_set,unordered_multiset
reference resources: cplusplus
These are written by the zipper method, so they have many similarities together. The following only records the member functions that have not been encountered before
You can see if you encounter something unclear C++_STL-map,multimap
1,std::unordered_map::bucket
size_type bucket ( const key_type& k ) const;
1.1 functions
Returns the bucket number of the key value k, which can be understood as the array subscript of the zipper method. The bucket is a slot in the hash table inside the container, and the element is allocated to the slot according to the hash value of its key. The bucket number ranges from 0 to (bucket_count-1). You can use unordered_map::begin and unordered_ The range iterator returned by map:: end accesses each element in the bucket.
1.2 parameters
K: key
1.3 return value
Returns the bucket number of the key value k
// unordered_map::bucket #include <iostream> #include <string> #include <unordered_map> int main () { std::unordered_map<std::string,std::string> mymap = { {"us","United States"}, {"uk","United Kingdom"}, {"fr","France"}, {"de","Germany"} }; for (auto& x: mymap) { std::cout << "Element [" << x.first << ":" << x.second << "]"; std::cout << " is in bucket #" << mymap.bucket (x.first) << std::endl; } return 0; }
2,std::unordered_map::bucket_count
size_type bucket_count() const noexcept;
2.1 functions
Returns the number of buckets. The number of buckets directly affects the load factor of the container hash table (thus affecting the probability of conflict). In order to keep the load factor below a certain threshold (its max_load_factor), the container automatically increases the number of buckets and causes re hashing each time it needs to increase the number of buckets.
Load factor
l o a d _ f a c t o r = s i z e / b u c k e t _ c o u n t load\_factor= size / bucket\_count load_factor=size/bucket_count
2.2 return value
Number of barrels
// unordered_map::bucket_count #include <iostream> #include <string> #include <unordered_map> int main () { std::unordered_map<std::string,std::string> mymap = { {"house","maison"}, {"apple","pomme"}, {"tree","arbre"}, {"book","livre"}, {"door","porte"}, {"grapefruit","pamplemousse"} }; unsigned n = mymap.bucket_count(); std::cout << "mymap has " << n << " buckets.\n"; for (unsigned i=0; i<n; ++i) { std::cout << "bucket #" << i << " contains: "; for (auto it = mymap.begin(i); it!=mymap.end(i); ++it) std::cout << "[" << it->first << ":" << it->second << "] "; std::cout << "\n"; } return 0; }
3,std::unordered_map::bucket_size
size_type bucket_size ( size_type n ) const;
3.1 functions
Returns the number of elements in bucket n.
3.2 return value
Number of elements in bucket n.
// unordered_map::bucket_size #include <iostream> #include <string> #include <unordered_map> int main () { std::unordered_map<std::string,std::string> mymap = { {"us","United States"}, {"uk","United Kingdom"}, {"fr","France"}, {"de","Germany"} }; unsigned nbuckets = mymap.bucket_count(); std::cout << "mymap has " << nbuckets << " buckets:\n"; for (unsigned i=0; i<nbuckets; ++i) { std::cout << "bucket #" << i << " has " << mymap.bucket_size(i) << " elements.\n"; } return 0; }
4,std::unordered_map::hash_function
hasher hash_function() const;
4.1 functions
Get unordered_map hash function object
The hash function is a unary function that accepts a key_type object as a parameter and returns a size based on it_ Unique value of type T. It is taken by the container at construction time (see the constructor of unordered_map for more information). By default, it is the default hash function for the corresponding key type: hash < key_type>.
// unordered_map::hash_function #include <iostream> #include <string> #include <unordered_map> typedef std::unordered_map<std::string,std::string> stringmap; int main () { stringmap mymap; stringmap::hasher fn = mymap.hash_function(); std::cout << "this: " << fn ("this") << std::endl; std::cout << "thin: " << fn ("thin") << std::endl; return 0; }
5,std::unordered_map::load_factor,max_bucket_count,max_load_factor
float load_factor() const noexcept; size_type max_bucket_count() const noexcept; //get (1) obtain float max_load_factor() const noexcept; //set (2) set up void max_load_factor ( float z );
5.1 functions
load_factor: return unordered_ The current load factor in the map container.
max_bucket_count: return unordered_ The maximum number of buckets a map container can have.
max_load_factor: get: return unordered_ The current maximum load factor for the map container. Set: set z to unordered_ The new maximum load factor for the map container. By default, unordered_ Max of map container_ load_ Factor is 1.0.
// unordered_map hash table stats #include <iostream> #include <unordered_map> int main () { std::unordered_map<int,int> mymap; std::cout << "size = " << mymap.size() << std::endl; std::cout << "bucket_count = " << mymap.bucket_count() << std::endl; std::cout << "load_factor = " << mymap.load_factor() << std::endl; std::cout << "max_load_factor = " << mymap.max_load_factor() << std::endl; return 0; }
6,std::unordered_map::rehash,reserve
void reserve ( size_type n ); void rehash( size_type n );
6.1 functions
reserve: set the minimum number of elements contained in each bucket
rehash: set the number of buckets in the container to n or more.
// unordered_map::reserve #include <iostream> #include <string> #include <unordered_map> int main () { std::unordered_map<std::string,std::string> mymap; mymap.reserve(6); mymap["house"] = "maison"; mymap["apple"] = "pomme"; mymap["tree"] = "arbre"; mymap["book"] = "livre"; mymap["door"] = "porte"; mymap["grapefruit"] = "pamplemousse"; for (auto& x: mymap) { std::cout << x.first << ": " << x.second << std::endl; } return 0; }
// unordered_map::rehash #include <iostream> #include <string> #include <unordered_map> int main () { std::unordered_map<std::string,std::string> mymap; mymap.rehash(20); mymap["house"] = "maison"; mymap["apple"] = "pomme"; mymap["tree"] = "arbre"; mymap["book"] = "livre"; mymap["door"] = "porte"; mymap["grapefruit"] = "pamplemousse"; std::cout << "current bucket_count: " << mymap.bucket_count() << std::endl; return 0; }
Unordered repeatable hash table_ multimap
Here we mainly talk about and unordered_ Different parts of map
1. key is not repeated, value is repeatable
2. No [] direct access element, no at()
The rest are unordered_ The map is similar, only slightly different, specific details unordered_multimap)
No sequel unordered_set
Here we mainly talk about the differences between and map
1. Key and value are the same, or only key, non repeatable and constant
2. No [] direct access element, no at()
The rest are unordered_ The map is similar, only slightly different, specific details unordered_set
Repeatable no sequel unordered_multiset
1. Key and value are the same, or only key, repeatable and constant
2. No [] direct access element, no at()
The rest are unordered_ The map is similar, only slightly different, specific details unordered_multiset