std::flat_set::contains(3)

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

πŸ’… std::flat_set::contains(3) β€” the C++ Standard Library glow-up

okay so bestie, we're in std::flat_set::contains(3) territory and honestly? this one ate. it's a little member function that just tells you whether something's in your flat_set, no cap. C++ Standard Library energy, straight from cppreference, and i'm about to make it make sense 🫢


✨ NAME ✨

std::flat_set::contains β€” literally just std::flat_set::contains. iconic, self-explanatory, no notes πŸ’…


🌸 Synopsis 🌸

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

so here's the tea on the two overloads:

  1. checks if there is an element with key equivalent to key in the container. that's the whole vibe πŸ”₯
  2. checks if there is an element with key that compares equivalent to the value x.

now the important fine print, don't skip this bestie: this overload participates in overload resolution only if the qualified-id

Compare::is_transparent

is valid and denotes a type. and the whole point of that? it allows calling this function without constructing an instance of Key ✨ efficient queen behavior, honestly πŸ‘‘


πŸŽ€ Parameters πŸŽ€

  • key β€” key value of the element to search for. that's the thing you're looking for, periodt.
  • x β€” a value of any type that can be transparently compared with a key. transparently, iykyk πŸ¦‹

πŸ’– Return value πŸ’–

true if there is such an element, otherwise false. binary. simple. slay πŸ“


🌈 Complexity 🌈

Logarithmic in the size of the container. log n, not n, we love to see it πŸ˜­πŸ’œ


🧚 Example 🧚

// Run this code

#include <iostream>
#include <flat_set>

int main()
{
    std::flat_set<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";
}

like, look at her go. {1, 2, 3, 4} is the set, and we're vibe-checking 2 and 5 against it. 2 is in there β€” main character. 5? not invited to the party πŸ™ˆ


πŸ¦‹ Output πŸ¦‹

2: Found
5: Not found

exactly what we predicted, no surprises, we stay winning ⭐


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

all three are public member functions, all three are besties, all three are giving "i also know things about this container" πŸ’€πŸ’–


real talk: both overloads are since C++23 β€” if your compiler is acting brand new and doesn't know what contains is, that's why, check your standard bestie 🌸

okay that's the whole page, mwah πŸ’‹ β€” this gem is from http://cppreference.com, dated 2024.06.10, filed under std::flat_set::contains(3). go forth and contains responsibly, ily 🫢✨

πŸ“¦ where this page comes from