std::flat_set::contains(3)
π 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:
- checks if there is an element with key equivalent to
keyin the container. that's the whole vibe π₯ - 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 aresince C++23β if your compiler is acting brand new and doesn't know whatcontainsis, 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms