std::unorde...iset::count(3)
π NAME π
std::unordered_multiset::count β yes bestie, that's the whole name, and yes, this is the C++ Standard Library manual, page 3, straight from cppreference.com. She does one thing and she does it iconic: she counts how many elements in your unordered_multiset match a key. That's the vibe check. π
β¨ Synopsis β¨
size_type count( const Key& key ) const; (1) (since C++11)
template< class K > (2) (since C++20)
size_type count( const K& x ) const;
- Returns the number of elements with key that compares equal to the specified argument
key. π«Ά - Returns the number of elements with key that compares equivalent to the specified argument
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 flex β¨
Not me pointing out that overload (1) is chilling since C++11 and overload (2) didn't pull up until C++20. Growth. π±
πΈ Parameters πΈ
keyβ key value of the elements to countxβ a value of any type that can be transparently compared with a key
π Return value π
- Number of elements with key
key. - Number of elements with key that compares equivalent to
x.
π₯ Complexity π₯
Linear in the number of elements with key key on average, worst case linear in the size of the container. So likeβ¦ usually she's fast, but the bad timeline is always lurking. π
π¦ Notes π¦
Feature-test macro girlies, gather round, there's exactly one and she's serving:
__cpp_lib_generic_unordered_lookupβ value201811L, Std(C++20), feature: Heterogeneous comparison lookup in unordered associative containers, overload (2)
iykyk. π
π§ Example π§
// Run this code
#include <algorithm>
#include <iostream>
#include <unordered_set>
int main()
{
std::unordered_multiset set{2, 7, 1, 8, 2, 8, 1, 8, 2, 8};
std::cout << "The set is:\n";
for (int e : set)
std::cout << e << ' ';
const auto [min, max] = std::ranges::minmax(set);
std::cout << "\nNumbers [" << min << ".." << max << "] frequency:\n";
for (int i{min}; i <= max; ++i)
std::cout << i << ':' << set.count(i) << "; ";
std::cout << '\n';
}
β Possible output β
The set is:
8 8 8 8 1 1 7 2 2 2
Numbers [1..8] frequency:
1:2; 2:3; 3:0; 4:0; 5:0; 6:0; 7:1; 8:4;
Look at those zeroes for 3, 4, 5, and 6 β literally not a single one in the building. π
π See also π
findβ finds element with specific key (public member function)containsβ checks if the container contains element with specific key (C++20) (public member function)equal_rangeβ returns range of elements matching a specific key (public member function)
okay that's the whole page, bestie β go forth and count π
source: http://cppreference.com Β· dated 2024.06.10 Β· std::unorde...iset::count(3) β mwah, drink water, and don't forget overload (2) needs those transparent type traits or she simply will not compile for you ππ§ββοΈ
π¦ 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