public member function
<unordered_map>

std::unordered_multimap::count

size_type count ( const key_type& k ) const;
計算具有特定鍵的元素
搜尋容器中鍵為 k 的元素,並返回找到的元素數量。

引數

k
要搜尋的鍵值。
成員型別key_type是容器中元素的鍵的型別,在 unordered_multimap 中定義為其第一個模板引數().

返回值

容器中鍵等於 k 的元素數量。

成員型別size_type是一種無符號整型型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// unordered_multimap::count
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_multimap<std::string,std::string> myumm = {
     {"orange","FL"},
     {"strawberry","LA"},
     {"strawberry","OK"},
     {"pumpkin","NH"} };

  for (auto& x: {"orange","lemon","strawberry"}) {
    std::cout << x << ": " << myumm.count(x) << " entries.\n";
  }

  return 0;
}

輸出
orange: 1 entries.
lemon: 0 entries.
strawberry: 2 entries.


複雜度

平均情況:與計數元素數量成線性關係。
最壞情況:相對於 容器大小 線性。

迭代器有效性

沒有變化。

另見