描述
C++ 函數std::map::count()返回與鍵關聯的映射值的數量k。
由於此容器不允許重複,因此值始終為 0 或 1。
聲明
以下是 std::map::count() 函數形式 std::map 頭的聲明。
C++98
size_type count (const key_type& k) const;
參數
k─ 搜索操作的鍵。
返回值
如果容器具有與鍵關聯的值,則返回 1k否則為 0。
異常
該成員函數不會拋出異常。
時間複雜度
對數,即 log(n)。
示例
下麵的例子展示了 std::map::count() 函數的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Initializer_list constructor */
map<char, int> m = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
};
if (m.count('a') == 1) {
cout << "m['a'] = " << m.at('a') << endl;
}
if (m.count('z') == 0) {
cout << "Value not present for key m['z']" << endl;
}
return 0;
}
讓我們編譯並運行上麵的程序,這將產生以下結果——
m['a'] = 1 Value not present for key m['z']
相關用法
- C++ Map count()用法及代碼示例
- C++ Map crend()用法及代碼示例
- C++ Map clear()用法及代碼示例
- C++ Map cend()用法及代碼示例
- C++ Map crbegin()用法及代碼示例
- C++ Map cbegin()用法及代碼示例
- C++ Map erase()用法及代碼示例
- C++ Map rend()用法及代碼示例
- C++ Map multimap()用法及代碼示例
- C++ Map emplace()用法及代碼示例
- C++ Map at()用法及代碼示例
- C++ Map size()用法及代碼示例
- C++ Map end()用法及代碼示例
- C++ Map get_allocator()用法及代碼示例
- C++ Map max_size()用法及代碼示例
- C++ Map begin()用法及代碼示例
- C++ Map map()用法及代碼示例
- C++ Map rbegin()用法及代碼示例
- C++ Map upper_bound()用法及代碼示例
- C++ Map key_comp()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Map Library - count() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。