
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ unordered_multimap::reserve() Function
The C++ std::unordered_multimap::reserve() function is used to sets the number of buckets in the container to the most appropriate to contain at least n elements without exceeding maximum load factor and rehashes the container.
If n is greater than the current bucket_count() * max_load_factor() then the containers bucket count is increased and a rehash is forced similarly if n is lower than that, the function may have no effect.
Syntax
Following is the syntax for std::unordered_multimap::reserve() function.
void reserve(size_type n);
Parameters
- n − It indicates the capacity of the container.
Return value
This function does not return anything.
Example 1
In the following example, let's see the usage of unordered_multimap::reserve() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<char, int> umm; cout << "Initial bucket count = " << umm.bucket_count() << endl; umm.reserve(5); cout << "Bucket count after reserve = " << umm.bucket_count() << endl; return 0; }
Output
On executing the above code, we get the following output, which shows the initial bucket count and reserve count −
Initial bucket count = 1 Bucket count after reserve = 5
Example 2
Consider the following example, where we are going to use the reserve() function to make the bucket count to store at least 5 element.
#include <iostream> #include <unordered_map> using namespace std; int main () { unordered_multimap<string, string> umMap; cout << "Bucket Count: " << umMap.bucket_count() << endl; umMap.reserve(5); cout << "Bucket Count after reserve(): " << umMap.bucket_count() << endl; umMap.insert({ {"Fname", "Tutorix"}, {"Fname", "tutorials"},{"Lname", "Point"}, {"Country","India"}, {"Locaton","Hyderabad"}}); for (auto& it: umMap) cout << it.first << "->" << it.second << endl; return 0; }
Output
On executing the above code, we get the bucket count in both the initial and reserve containers, as well as the element of the container.
Bucket Count: 1 Bucket Count after reserve(): 5 Country->India Lname->Point Locaton->Hyderabad Fname->tutorials Fname->Tutorix
Example 3
Let's look at the following example, where we are going to display the bucket with its elements before and after the use of the reserve() function.
#include <iostream> #include <unordered_map> using namespace std; int main () { unordered_multimap<string, string> umMap={{"Hyderabad", "India"}, {"Delhi", "India"}, {"Bangalore", "India"}, {"Hyderabad", "Telngana"}}; cout<<"Unordered_multimap contains "<<umMap.bucket_count()<<" buckets:"; for(unsigned int i = 0; i < umMap.bucket_count(); i++) { cout<<"\nThe bucket "<<i<<" contains: "; for(auto it = umMap.begin(i); it != umMap.end(i); ++it) { cout<<it->first<<":"<<it->second<<" "; } } cout<<"\nCapacity is changed using reserve function.\n"; umMap.reserve(5); cout<<"Unordered_multimap contains "<<umMap.bucket_count()<<" buckets:"; for(unsigned int i = 0; i < umMap.bucket_count(); i++) { cout<<"\nThe bucket "<<i<<" contains: "; for(auto it = umMap.begin(i); it != umMap.end(i); ++it) { cout<<it->first<<":"<<it->second<<" "; } } return 0; }
Output
On executing the above code, we get the number of buckets in both initial and reserve conditions and their elements −
Unordered_multimap contains 5 buckets: The bucket 0 contains: The bucket 1 contains: Bangalore:India The bucket 2 contains: The bucket 3 contains: Delhi:India The bucket 4 contains: Hyderabad:Telngana Hyderabad:India Capacity is changed using reserve function. Unordered_multimap contains 5 buckets: The bucket 0 contains: The bucket 1 contains: Bangalore:India The bucket 2 contains: The bucket 3 contains: Delhi:India The bucket 4 contains: Hyderabad:Telngana Hyderabad:India