std::unorde...t::contains(3)
β¨ std::unordered_set::contains β¨
okay so bestie, welcome to the <std::unorde...t::contains(3)> page from the C++ Standard Libary (yes, thatβs how itβs spelled up top, and weβre respecting the lore π
). This whole page is about checking whether a key is in an unordered_set, and it is lowkey iconic.
π NAME π
std::unordered_set::contains β std::unordered_set::contains
πΈ Synopsis πΈ
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 is an element with key equivalent to
keyin the container. π - (2) Checks if there is an element with key that compares equivalent to the value
x. This overload participates in overload resolution only ifHash::is_transparentandKeyEqual::is_transparentare valid and each denotes a type. This assumes that suchHashis callable with bothKandKeytype, and that theKeyEqualis transparent, which, together, allows calling this function without constructing an instance ofKey. no cap, thatβs the whole transparent-comparison vibe π¦
π¦ 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. simple as that, bestie β¨
π₯ Complexity π₯
Constant on average, worst case linear in the size of the container. so average girlie is fast β‘ and worst case is giving βweβre checking the whole containerβ energy π
π Example π
// Run this code
#include <iostream>
#include <unordered_set>
int main()
{
std::unordered_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";
}
π 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).
okay thatβs the page, bestie π«Ά source/version/date: http://cppreference.com, 2024.06.10, and the manual-page label is std::unorde...t::contains(3). mwah πβ¨
π¦ 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