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

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿ’– NAME ๐Ÿ’–

std::unordered_multiset::find โ€” bestie, it's literally std::unordered_multiset::find, that's the whole name, no notes ๐Ÿ’…

This one lives in the C++ Standard Libary manual, and the page itself is std::unorde...ltiset::find(3) (yes the title is truncated with dots, she's mysterious like that โœจ).


โœจ Synopsis โœจ

okay so here's the whole menu, look at her go:

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;
  • (1), (2) โ€” these two look for an element whose key is equivalent to key. โš ๏ธ And here's the tea: if the container has several elements with the key you asked for, any of them may be returned. Not the first, not the last, any. So don't get attached to a specific one, bestie ๐Ÿ˜ญ
  • (3), (4) โ€” these look for an element whose key compares equivalent to the value x. But she's picky about showing up: this overload only participates in overload resolution if Hash::is_transparent and KeyEqual::is_transparent are both 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 โ€” and together? That's what lets you call this function without constructing an instance of Key. No cap, that's the whole point, that's the vibe ๐Ÿฆ‹

๐ŸŽ€ Parameters ๐ŸŽ€

  • key โ€” the key value of the element you're searching for ๐Ÿ”
  • x โ€” a value of any type that can be transparently compared with a key โœจ

๐ŸŒธ Return value ๐ŸŒธ

An iterator to the element you requested, periodt. ๐Ÿ’…

But if no such element is found? You get the past-the-end iterator (go peek at end(), she's right there) ๐Ÿซถ


๐Ÿ”ฅ Complexity ๐Ÿ”ฅ

  • Constant on average โญ
  • Worst case linear in the size of the container ๐Ÿ’€

๐Ÿงš Notes ๐Ÿงš

There's a feature-test macro for the heterogeneous girlies, and here's the whole row, not a single crumb left out:

  • Feature-test macro: __cpp_lib_generic_unordered_lookup
  • Value: 201811L
  • Std: (C++20)
  • Feature: Heterogeneous comparison lookup in unordered associative containers; overloads (3,4) ๐Ÿ‘‘

iykyk ๐ŸŒˆ


๐Ÿ“ Example ๐Ÿ“

// Run this code

#include <iostream>
#include <unordered_set>

int main()
{
    // Simple comparison demo.
    std::unordered_multiset<int> example = {1, 2, 3, 4};

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

๐ŸŒˆ Output ๐ŸŒˆ

Found 2

she ate, honestly ๐Ÿ‘‘


๐Ÿ’œ See also ๐Ÿ’œ

  • count โ€” returns the number of elements matching a specific key (public member function) ๐Ÿงฎ
  • equal_range โ€” returns range of elements matching a specific key (public member function) ๐Ÿ“

okay that's the whole page, mwah ๐Ÿ’‹ โ€” sourced straight from http://cppreference.com, dated 2024.06.10, and it's the std::unorde...ltiset::find(3) page, go forth and find your elements bestie ๐Ÿฆ‹โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from