std::unorde...ltimap::find(3)

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

πŸ’– std::unordered_multimap::find πŸ’–

okay so bestie, this whole page is for std::unorde...ltimap::find(3) from the C++ Standard Libary manual β€” yes, the header title is literally truncated like that, no cap. We’re talking about std::unordered_multimap::find, and it’s here to help you find an element by key. periodt. ✨

🌸 NAME 🌸

std::unordered_multimap::find β€” that’s the name, that’s the whole assignment, and honestly? Iconic. πŸ’…

✨ Synopsis ✨

iterator find( const Key& key );             (1) (since C++11)
const_iterator find( const Key& key ) const; (2) (since C++11)
template< class K >                          (3) (since C++20)
iterator find( const K& x );
template< class K >                          (4) (since C++20)
const_iterator find( const K& x ) const;

So here’s the overload tea, bestie:

  • (1), (2) β€” finds an element with key equivalent to key. If there are several elements with the requested key in the container, any of them may be returned. This is since C++11.
  • (3), (4) β€” finds 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. This is since C++20.

πŸŽ€ 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 πŸ¦‹

An iterator to the requested element. If no such element is found, past-the-end (see end()) iterator is returned. iykyk πŸ‘‘

πŸ’… Complexity πŸ’…

Constant on average, worst case linear in the size of the container. Not me memorizing that for the vibe check 😭

🌈 Notes 🌈

Feature-test macro moment, bestie:

  • __cpp_lib_generic_unordered_lookup β€” Value 201811L, Std (C++20), Feature: β€œHeterogeneous comparison lookup in unordered associative containers; overloads (3,4)”.

πŸ“ Example πŸ“

The example literally starts with // Run this code, so run it, bestie:

        #include <iostream>
        #include <unordered_map>

        int main()
        {
            // Simple comparison demo.
            std::unordered_multimap<int,char> example = {{1,'a'}, {2,'b'}};

            if (auto search = example.find(2); search != example.end())
                std::cout << "Found " << search->first << ' ' << search->second
       << '\n';
            else
                std::cout << "Not found\n";
        }

Output:

Found 2 b

πŸ‘‘ See also πŸ‘‘

  • 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).

okay that’s the whole page, bestie 🫢 source: http://cppreference.com; version/date: 2024.06.10; and the footer title is std::unorde...ltimap::find(3). Slay this lookup responsibly. πŸ’–βœ¨

πŸ“¦ where this page comes from