![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- 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>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_set::max_size() Function
The C++ std::unordered_set::max_size() function is used to return the maximum number of elements that the unordered_set container can hold due to system and library implementation limitations.
The maximum size will always be the same in the particular program; it does not depend upon the unordered_set size or type, whether the unordered_set is empty or not.
Syntax
Following is the syntax of std::unordered_set::max_size() function.
size_type max_size() const noexcept;
Parameters
This function does not accepts any parameter.
Return Value
This function returns the maximum number of elements that the unordered_set container can hold.
Example 1
Consider the following example, where we are going to demonstrate the usage of unordered_set::max_size() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myset; std::cout << "0. size: " << myset.max_size() << std::endl; myset = {"milk","potatoes","eggs"}; std::cout << "1. size: " << myset.max_size() << std::endl; myset.insert ("pineapple"); std::cout << "2. size: " << myset.max_size() << std::endl; myset.erase ("milk"); std::cout << "3. size: " << myset.max_size() << std::endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
0. size: 768614336404564650 1. size: 768614336404564650 2. size: 768614336404564650 3. size: 768614336404564650
Example 2
Let's look at the following example, where we are going to use the unordered_set::max_size() function to get the maximum number of sizes that a container can hold.
#include <iostream> #include <iostream> #include <locale> #include <unordered_set> using namespace std; int main() { unordered_set<int> uSet = {1, 2, 3}; // Check if the locale is available on the system try { cout.imbue(locale("en_US.UTF-8")); } catch (const std::runtime_error& e) { cout << "Locale not found: " << e.what() << '\n'; // Fallback to the default locale cout.imbue(locale("")); } cout << "Maximum size of an unordered_set is " << uSet.max_size() << '\n'; return 0; }
Output
If we run the above code it will generate the following output −
Locale not found: locale::facet::_S_create_c_locale name not valid Maximum size of an unordered_set is 576460752303423487
Example 3
In the following example, we are going to consider the unordered_sets that contain integer type and applying the unordered_set::max_size() function to display the maximum size of both unordered_sets.
#include <iostream> #include <locale> #include <unordered_set> using namespace std; int main() { unordered_set<int> uSet, myUset; uSet={1, 2, 3}; myUset={2, 3, 4, 5}; cout << "Maximum size of a unordered_set first is " << uSet.max_size() << '\n'; cout << "Maximum size of a unordered_set second is " << myUset.max_size() << '\n'; }
Output
Following is the output of the above code −
Maximum size of a unordered_set first is 576460752303423487 Maximum size of a unordered_set second is 576460752303423487
Example 4
Following is another example of the usage of the unordered_set::max_size() function, where we are going to conider two unordered_sets, one is of int type and the other is char type, and display the maximum size of both unordered_sets.
#include <iostream> #include <locale> #include <unordered_set> using namespace std; int main() { unordered_set<int> uSet = {1, 2, 3}; unordered_set<char> myUset = {'a', 'b', 'c'}; cout << "Maximum size of a unordered_set first is " << uSet.max_size() << '\n'; cout << "Maximum size of a unordered_set second is " << myUset.max_size() << '\n'; }
Output
Output of the above code is as follows −
Maximum size of a unordered_set first is 576460752303423487 Maximum size of a unordered_set second is 576460752303423487