unordered_map rehash in C++ STL Last Updated : 18 May, 2022 Comments Improve Suggest changes Like Article Like Report The std::unordered_map::rehash() is a built in function C++ STL which sets the number of buckets in container to n or more. Syntax void rehash( size_type s );If s is greater than the current buckets into the container then rehashed is done. The new bucket count can be greater than or equal to n.If s is less than current bucket count then there may or may not be any effect of function. It totally depends upon compiler Parameters : It takes New number of buckets into the container. Return type : Its return type is none. Example-1: CPP // C++ code to illustrate the method // unordered_map rehash #include <bits/stdc++.h> using namespace std; int main() { unordered_map<char, int> sample; // Map initialization sample = { { 'a', 2 }, { 'b', 4 }, { 'c', 6 } }; cout << " Size of container : " << sample.size() << endl; cout << " Initial bucket count : " << sample.bucket_count() << endl; // changing rehash sample.rehash(30); cout << " Size of container : " << sample.size() << endl; cout << " Now bucket count is : " << sample.bucket_count() << endl; return 0; } Output:Size of container : 3 Initial bucket count : 5 Size of container : 3 Now bucket count is : 31 Example-2: CPP // C++ code to illustrate the method // unordered_map rehash #include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, int> sample; // Map initialization sample = { { 2, 2 }, { 3, 4 }, { 4, 6 }, { 5, 8 } }; cout << " Size of container : " << sample.size() << endl; cout << " Initial bucket count : " << sample.bucket_count() << endl; // changing rehash sample.rehash(20); cout << " Size of container : " << sample.size() << endl; cout << " Now bucket count is : " << sample.bucket_count() << endl; return 0; } Output:Size of container : 4 Initial bucket count : 7 Size of container : 4 Now bucket count is : 23 Comment More infoAdvertise with us Next Article bucket_count and bucket_size in unordered_map in C++ ankit15697 Follow Improve Article Tags : Technical Scripter C++ STL cpp-unordered_map cpp-unordered_map-functions +1 More Practice Tags : CPPSTL Similar Reads Unordered Map in C++ STL In C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not 7 min read Different Ways to Initialize an unordered_map in C++ Initialization is the process of assigning the initial values to the std::unordered_map elements. In this article, we will learn different methods to initialize the std::unordered_map in C++.Table of ContentUsing Initializer ListBy Inserting Elements One by OneFrom Another std::unordered_mapFrom Ano 3 min read Commonly Used Methodsunordered_map begin() in C++The unordered_map::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the unordered_map container or in any of its bucket. Syntax for first element in unordered_map container: unordered_map.begin() Parameters: This function does not accepts any param 2 min read unordered_map end( ) function in C++ STLThe unordered_map::end() is a built-in function in C++ STL which returns an iterator pointing to the position past the last element in the container in the unordered_map container. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all 3 min read unordered_map size() in C++ STLThe unordered_multimap::size() is a built-in function in C++ Standard Template Library which return's the number of element in the unordered map. Syntax: unordered_multimap_name.size() Return Value: It returns the number of the element present in the unordered map. Time complexity: Constant i.e. O(1 1 min read unordered_map insert in C++ STLThe std::unordered_map::insert() in C++ STL is a built-in function used to insert a key-value pair in unordered_map container. As unordered maps only store unique elements, this function does not insert elements with duplicate keys. In this article, we will learn about std::unordered_map::insert() i 4 min read unordered_map at() in C++In C++, unordered_map at() is a library function used to find the value associated with a given key in the unordered_map container. Let's look at an example to see how to use this function.C++#include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, string> um = {{1, 2 min read unordered_map find in C++ STLIn C++, std::unordered_map::find function is used to search for a specific element using the key in an unordered map container. It is a member function of std::unordered_map container defined inside <unordered_map> header file.Syntaxum.find(key);Parameterskey: The key of the element to be sear 1 min read unordered_map empty in C++ STLunordered_map::empty() function is used to check whether container size is zero or not. If container size is zero then it return TRUE otherwise it return FALSE. Syntax: unordered_map_name.empty() Parameters: This function does not accept any parameter Return type: This function returns boolean value 2 min read Other Member Methodsunordered_map max_size in C++ STLThe unordered_map::max_size is a built in function in C++ STL. It returns maximum number of elements which unordered_map can hold. Maximum number of elements in any container depends upon system and library implementation. Syntax size unordered_map.max_size() Parameters: It does not accept any param 2 min read unordered_map rehash in C++ STLThe std::unordered_map::rehash() is a built in function C++ STL which sets the number of buckets in container to n or more. Syntax void rehash( size_type s );If s is greater than the current buckets into the container then rehashed is done. The new bucket count can be greater than or equal to n.If s 2 min read bucket_count and bucket_size in unordered_map in C++Unordered_map is an associated container that stores elements formed by the combination of key-value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-define 4 min read unordered_map bucket() in C++ STLThe unordered_map::bucket() is a built-in STL function in C++ which returns the bucket number where the element with the key k is located in the map. Syntax: size_type bucket(key) Parameter: The function accepts one mandatory parameter key which specifies the key whose bucket number is to be returne 1 min read unordered_map load_factor in C++ STLThe unordered_map::load_factor() is a built-in function in C++ STL which returns the current load factor in the unordered_map container. The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count): load_factor = size / bucket_count 3 min read Traversing a Map and unordered_map in C++ STL The maps are described as mapped associative containers for elements where each element has a key and value assigned to it. Another form of map container seen in the C++ STL is the unordered map. It is the same as map containers just that they don't store the data in sorted order.We can traverse map 5 min read How to use unordered_map efficiently in C++ Pre-requisite: unordered_set,  unordered_map C++ provides std::unordered_set and std::unordered_map to be used as a hash set and hash map respectively. They perform insertion/deletion/access in constant average time. However, the worst-case complexity is O(n2).The reason is that the unordered_map 6 min read map vs unordered_map in C++ In C++, map and unordered_map are the containers that store can store data in the form of key-value pairs, but they differ significantly in terms of underlying implementation and performance characteristics.The below table lists the primary differences between map and unordered_map container:mapunor 2 min read How to create an unordered_map of tuples in C++? Tuple - A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. Unordered Map does not contain a hash function for a tuple. So if we want to hash a tuple the 2 min read How to create an unordered_map of user defined class in C++? unordered_map is used to implement hash tables. It stores key value pairs. For every key, a hash function is computed and value is stored at that hash entry. Hash functions for standard data types (int, char, string, ..) are predefined. How to use our own data types for implementing hash tables?unor 3 min read Like