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

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

🌸 std::unordered_multiset::contains(3) 🌸

okay bestie, so this is a page from the C++ Standard Library manual β€” the whole vibe is std::unordered_multiset::contains, and the manual page itself is literally called std::unorde...et::contains(3) (yes the truncation is part of the aesthetic, don't come for her πŸ’…).


✨ NAME ✨

std::unordered_multiset::contains β€” yeah. that's it. that's the name. it's giving "the function is the moment" πŸ‘‘


πŸ¦‹ Synopsis πŸ¦‹

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

so there's two flavors, both landing in C++20 era (iconic, we love a modern queen):

  1. Checks if there is an element with key equivalent to key in the container. simple. clean. slay.
  2. Checks if there is an element with key that compares equivalent to the value x.

and now the ✨fine print✨ for overload (2) β€” hear me out because this part actually matters:

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.

translation: it's the "no extra object construction, we're efficient in this house" overload 🫢 iykyk.


πŸŽ€ 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 πŸ’–

true if there is such an element, otherwise false. that's the whole plot. binary. black and white. no in-between bestie 🎯


πŸ”₯ Complexity πŸ”₯

Constant on average, worst case linear in the size of the container. so like, usually she's fast, but worst case she's doing the whole walk-through. ngl keep that in mind 😭


🌈 Example 🌈

// Run this code

#include <iostream>
#include <unordered_set>

int main()
{
    std::unordered_multiset<int> example{1, 2, 3, 4};

    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

see? 2 ate, 5 did not. periodt πŸ’€βœ¨


πŸ‘‘ 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)

okay that's the whole page, no notes 🧚 and for the cute sign-off: this came from http://cppreference.com and the page is dated 2024.06.10 β€” the footer's out here repping std::unorde...et::contains(3) one more time for the girls πŸ’œπŸ«Ά

πŸ“¦ where this page comes from