std::unordered_map::count(3)
π std::unordered_map::count(3) π
okay bestie, youβre on the std::unordered_map::count page from the C++ Standard Libary manual, and itβs literally the βhow many of this key do we actually have?β girlie. No cap, sheβs concise, sheβs useful, and she does not allow duplicates β periodt. π
π NAME π
std::unordered_map::count β std::unordered_map::count
β¨ 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, which is either0or1since this container does not allow duplicates. - 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.
π 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, that is either1or0. - Number of elements with key that compares equivalent to
x.
π¦ Complexity π¦
Constant on average, worst case linear in the size of the container. So: average is chill, worst case is giving βwhole container walk,β iykyk.
π Notes π
Feature-test macro tea β:
- Feature-test macro:
__cpp_lib_generic_unordered_lookup - Value:
201811L - Std:
(C++20) - Feature: Heterogeneous comparison lookup in unordered associative containers, overload (2)
π Example π
Run this code π
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
std::unordered_map<int, std::string> dict = {
{1, "one"}, {6, "six"}, {3, "three"}
};
dict.insert({4, "four"});
dict.insert({5, "five"});
dict.insert({6, "six"});
std::cout << "dict: { ";
for (auto const& [key, value] : dict)
std::cout << '[' << key << "]=" << value << ' ';
std::cout << "}\n\n";
for (int i{1}; i != 8; ++i)
std::cout << "dict.count(" << i << ") = " << dict.count(i) <<
'\n';
}
Possible output:
dict: { [5]=five [4]=four [1]=one [6]=six [3]=three }
dict.count(1) = 1
dict.count(2) = 0
dict.count(3) = 1
dict.count(4) = 1
dict.count(5) = 1
dict.count(6) = 1
dict.count(7) = 0
π 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)
thatβs the whole page, bestie π source: http://cppreference.com Β· version/date: 2024.06.10 Β· manual page: std::unordered_map::count(3) π«Ά
π¦ 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