std::unorde...iset::count(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ 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;
  1. Returns the number of elements with key that compares equal to the specified argument key. 🫢
  2. Returns the number of elements with key that compares equivalent to the specified argument x. This overload participates in overload resolution only if Hash::is_transparent and KeyEqual::is_transparent are valid and each denotes a type. This assumes that such Hash is callable with both K and Key type, and that the KeyEqual is transparent, which, together, allows calling this function without constructing an instance of Key. 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 count
  • x β€” a value of any type that can be transparently compared with a key

πŸ’– Return value πŸ’–

  1. Number of elements with key key.
  2. 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 β€” value 201811L, 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