std::unorde...ap::contains(3)

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

okay so bestie, we're looking at std::unordered_multimap::contains from the C++ Standard Libary β€” yes, section 3, and yes I'm keeping the typo because it's iconic and I don't make the rules πŸ’…. This page is short, it's sweet, and honestly? It ate. Let's get into it ✨

🌸 NAME 🌸

std::unordered_multimap::contains β€” that's it, that's the name. The original literally just repeats it twice like "std::unordered_multimap::contains - std::unordered_multimap::contains" and I respect the commitment to the bit πŸ’€πŸ‘‘


✨ Synopsis ✨

          bool contains( const Key& key ) const; (1) (since C++20)
          template< class K >                    (2) (since C++20)
          bool contains( const K& x ) const;

Two overloads, both since C++20 β€” so if you're stuck on an older standard, sorry bestie, this one's not for you 😭

  1. Checks if there is an element with key equivalent to key in the container. Simple, clean, does exactly what it says on the tin πŸ”
  2. Checks if there is an element with key that compares equivalent to the value x. This overload participates in overload resolution only if Hash::is_transparent and KeyEqual::is_transparent are valid and each denotes a type. This assumes that such Hash is callable with both K and Key type, and that the KeyEqual is transparent, which, together, allows calling this function without constructing an instance of Key. Like, it's transparent, it's lowkey doing the most so you don't have to build a whole Key object β€” main character energy fr fr 🌟

πŸŽ€ Parameters πŸŽ€

  • key β€” key value of the element to search for πŸ”
  • x β€” a value of any type that can be transparently compared with a key πŸ¦‹

πŸ’œ Return value πŸ’œ

Returns true if there is such an element, otherwise false. That's it. Boolean couture, periodt πŸ’…


πŸ”₯ Complexity πŸ”₯

Constant on average, worst case linear in the size of the container. So usually it's fast and slay, but in the worst case it's giving "oh no bestie" 😭


🌈 Example 🌈

// Run this code

        #include <iostream>
        #include <unordered_map>

        int main()
        {
            std::unordered_multimap<int, char> example{{1, 'a'}, {2, 'b'}};

            for (int x : {2, 5})
                if (example.contains(x))
                    std::cout << x << ": Found\n";
                else
                    std::cout << x << ": Not found\n";
        }

⭐ Output ⭐

        2: Found
        5: Not found

Look at it go β€” 2 is found because it's literally in the multimap, and 5 is not found because... it's not. Shocking, I know πŸ’€βœ¨


🫢 See also 🫢

  • find β€” finds element with specific key (public member function) πŸ”
  • count β€” returns the number of elements matching specific key (public member function) πŸ”’
  • equal_range β€” returns range of elements matching a specific key (public member function) πŸ“

and that's the whole page bestie!! source is http://cppreference.com, version 2024.06.10, and the page title is std::unorde...ap::contains(3) β€” that's 3 as in section 3 of the manual, not a footnote, periodt. now go check your keys and slay πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from