std::multiset::contains(3)

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

๐ŸŒˆ std::multiset::contains โ€” C++ Standard Libary manual ๐Ÿ’… (section 3, bestie)

Okay so you opened the std::multiset::contains(3) page and yes, you're in the C++ Standard Libary manual โ€” iconic, we love a standard-library moment. โœจ


๐ŸŒธ NAME ๐ŸŒธ

It's literally std::multiset::contains โ€” that's the function name AND the page name. No plot twist, just main character energy. ๐ŸŽ€


๐Ÿฆ‹ Synopsis ๐Ÿฆ‹

Here's the whole signature situation, copied exactly so nobody comes for me:

bool contains( const Key& key ) const; (1) (since C++20)
template< class K >                    (2) (since C++20)
bool contains( const K& x ) const;
  1. Checks if there's an element with key equivalent to key in the container. ๐Ÿ‘‘
  2. Checks if there's an element with key that compares equivalent to the value x.

This overload only participates in overload resolution if the qualified-id Compare::is_transparent is valid and denotes a type. It lets you call this function without constructing an instance of Key. Slay, transparent comparison, slay. ๐Ÿ’…


๐Ÿงš 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 it. Simple. Clean. Periodt. ๐Ÿ’–


๐Ÿ”ฅ Complexity ๐Ÿ”ฅ

Logarithmic in the size of the container. So it's giving efficient, not chaotic. ๐ŸŒธ


๐ŸŽ€ Example ๐ŸŽ€

// Run this code

#include <iostream>
#include <set>

int main()
{
    std::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 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) ๐Ÿซถ

mwah ๐Ÿ’– this little gem lives at http://cppreference.com, dated 2024.06.10, and it is officially the std::multiset::contains(3) page โ€” bye bestie, go check those keys! ๐Ÿฆ‹๐ŸŽ€

๐Ÿ“ฆ where this page comes from