std::unorde...ap::contains(3)
okay so bestie, we're looking at std::unordered_multimap::contains from the C++ Standard Libary β yes, section 3, and yes I'm keeping the typo because it's iconic and I don't make the rules π
. This page is short, it's sweet, and honestly? It ate. Let's get into it β¨
πΈ NAME πΈ
std::unordered_multimap::contains β that's it, that's the name. The original literally just repeats it twice like "std::unordered_multimap::contains - std::unordered_multimap::contains" and I respect the commitment to the bit ππ
β¨ Synopsis β¨
bool contains( const Key& key ) const; (1) (since C++20)
template< class K > (2) (since C++20)
bool contains( const K& x ) const;
Two overloads, both since C++20 β so if you're stuck on an older standard, sorry bestie, this one's not for you π
- Checks if there is an element with key equivalent to
keyin the container. Simple, clean, does exactly what it says on the tin π - 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. Like, it's transparent, it's lowkey doing the most so you don't have to build a wholeKeyobject β main character energy fr fr π
π 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 π
Returns true if there is such an element, otherwise false. That's it. Boolean couture, periodt π
π₯ Complexity π₯
Constant on average, worst case linear in the size of the container. So usually it's fast and slay, but in the worst case it's giving "oh no bestie" π
π Example π
// Run this code
#include <iostream>
#include <unordered_map>
int main()
{
std::unordered_multimap<int, char> example{{1, 'a'}, {2, 'b'}};
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
Look at it go β 2 is found because it's literally in the multimap, and 5 is not found because... it's not. Shocking, I know πβ¨
π«Ά 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) π
and that's the whole page bestie!! source is http://cppreference.com, version 2024.06.10, and the page title is std::unorde...ap::contains(3) β that's 3 as in section 3 of the manual, not a footnote, periodt. now go check your keys and slay πβ¨π
π¦ 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