當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Map count()用法及代碼示例


描述

C++ 函數std::multimap::count()返回與鍵關聯的映射值的數量k

聲明

以下是 std::multimap::count() 函數形式 std::map 頭的聲明。

C++98

size_type count (const key_type& k) const;

參數

k─ 搜索操作的鍵。

返回值

返回與鍵關聯的值的數量。

異常

如果拋出異常,對容器沒有影響。

時間複雜度

對數,即 O(log n)

示例

下麵的例子展示了 std::multimap::count() 函數的用法。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Multimap with duplicates */
   multimap<char, int> m {
         {'a', 1},
         {'a', 2},
         {'b', 3},
         {'c', 4},
         {'c', 5},
               };

   cout << "count of 'a' = " << m.count('a') << endl;
   cout << "count of 'b' = " << m.count('b') << endl;
   cout << "count of 'c' = " << m.count('c') << endl;

   return 0;
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

count of 'a' = 2
count of 'b' = 1
count of 'c' = 2

相關用法


注:本文由純淨天空篩選整理自 C++ Map Library - count() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。