GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::unordered_multimap::count - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. unordered_map
  3. unordered_multimap
  4. count

最終更新日時: 2026年10月09日 12時31分16秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<unordered_map>

std::unordered_multimap::count(C++11)

size_type count(const key_type& x) const;           // (1) C++11
constexpr size_type count(const key_type& x) const; // (1) C++26

template <class K>
size_type count(const K& k) const;           // (2) C++20
template <class K>
constexpr size_type count(const K& k) const; // (2) C++26

概要

キーを検索し、コンテナ内に見つかった要素の数を返す。

  • (1) : キーxを検索し、合致する要素数を取得する
  • (2) : キーkを透過的に検索し、合致する要素数を取得する

(2)の透過的な検索は、Pred::is_transparentおよびHash::is_transparentが定義される場合に有効になる機能であり、例としてunordered_multimap<string, int> m;に対してm.count("key");のようにstring型のキーを持つ連想コンテナの検索インタフェースに文字列リテラルを渡した際、stringの一時オブジェクトが作られないようにできる。詳細はstd::hashクラスのページを参照。

パラメータ

  • x : 検索するキー値。key_type はメンバ型であり、unordered_multimap コンテナの中で Key の別名として定義される。ここで Key は 1 番目のテンプレートパラメータである。
  • k : 検索するキー。key_typeと透過的に比較可能な型K型のキーである。

テンプレートパラメータ制約

  • (2) : Pred::is_transparent型およびHash::is_transparent型が定義されていること

戻り値

指定されたキーと同じ値のキーの要素が見つかった要素数を返す。

メンバ型 size_type は符号なし整数型である。

例外

投げない。

計算量

xとkを共通の変数aであるとして、

  • 平均: O(count(a))
  • 最悪: size について線形時間

備考

  • 要素の有無を調べるだけであれば、この関数ではなくcontains()メンバ関数を使用する。この関数は等価なキーをもつ要素の個数に対して平均的に比例するが、contains()は平均的に定数時間である

例

#include <iostream>
#include <unordered_map>

int main()
{
  std::unordered_multimap<int, char> um;

  um.insert(std::make_pair(1, 'a'));
  um.insert(std::make_pair(1, 'b'));

  std::cout << um.count(1) << std::endl;
  std::cout << um.count(2) << std::endl;

  return 0;
}

出力

2
0

バージョン

言語

  • C++11

処理系

  • Clang: 9.0.1 ✅
  • GCC: ??
  • Visual C++: 2012 ✅

関連項目

名前 説明
find 指定したキーで要素を探す
size 要素数を取得する

参照

  • LWG Issue 2304. Complexity of count in unordered associative containers
  • P0919R3 Heterogeneous lookup for unordered containers
  • P3372R3 constexpr containers and adaptors
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.