std::unorde...t::max_size(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME

std::unordered_set::max_size β€” literally what it says on the tin, bestie: the max size. That's the whole vibe. πŸŽ€

Oh and just so we're clear, we're moving through the C++ Standard Libary today, mwah.


✨ Synopsis

size_type max_size() const noexcept;  (since C++11)

Returns the maximum number of elements the container is able to hold due to system or library implementation limitations β€” i.e. std::distance(begin(), end()) for the largest container. That's the biggest one. The final boss container. πŸ‘‘


🌸 Parameters

None. Nada. Zero. Nothing to pass in β€” she works alone, main character energy fr fr. πŸ’…


πŸ’– Return value

The maximum number of elements. That's it, that's the return. 🫢


⚑ Complexity

Constant. Like, O(1) constant, no cap β€” she's not out here doing loops. Iconic behaviour honestly.


πŸ¦‹ Notes

Okay so this value typically reflects the theoretical limit on the size of the container β€” at most std::numeric_limits<difference_type>::max(). Key word: theoretical. πŸ™ˆ

At runtime though, the size of the container may be limited to a value smaller than max_size() by the amount of RAM available. So like, your dreams may be bigger than your memory. Story of my life. 😭


πŸ“ Example

// Run this code

#include <iostream>
#include <locale>
#include <unordered_set>

int main()
{
    std::unordered_set<char> p;
    std::unordered_set<long> q;

    std::cout.imbue(std::locale("en_US.UTF-8"));
    std::cout << std::uppercase
              << "p.max_size() = " << std::dec << p.max_size() << " = 0x"
              << std::hex << p.max_size() << '\n'
              << "q.max_size() = " << std::dec << q.max_size() << " = 0x"
              << std::hex << q.max_size() << '\n';
}

🌈 Possible output

p.max_size() = 1,152,921,504,606,846,975 = 0xFFF,FFF,FFF,FFF,FFF
q.max_size() = 1,152,921,504,606,846,975 = 0xFFF,FFF,FFF,FFF,FFF

Look at those numbers. Comma-separated. Hex on the side. It's giving flex, honestly. πŸ”₯


πŸ‘‘ See also

  • size β€” returns the number of elements (public member function)

<div style="text-align:center">β€” 🧚 β€”</div>

Okay bestie that's the whole page! This came straight from http://cppreference.com and it's dated 2024.06.10, so if anything looks different later, don't come for me. Go forth and check your max_size(), you got this πŸ’œβœ¨

πŸ“¦ where this page comes from