目录

map::count

描述 (Description)

C ++函数std::map::count()返回与键k关联的映射值的数量。

由于此容器不允许重复,因此值始终为0或1。

声明 (Declaration)

以下是std :: map :: count()函数形式std :: map头的声明。

C++98

size_type count (const key_type& k) const;

参数 (Parameters)

k - 搜索操作的键。

返回值

如果容器具有与键k相关联的值,则返回1,否则返回0。

异常 (Exceptions)

该成员函数不会抛出异常。

时间复杂

对数即log(n)。

例子 (Example)

以下示例显示了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']
↑回到顶部↑
WIKI教程 @2018