我们要在 STL 中的 multimap 容器中查找某个键的个数,我们可以使用 count 函数,传入我们需要查找的键名即可。
count(key)
参数 | 描述 |
---|---|
key | 需要查找的键。 |
返回键 key 在 multimap 中的个数。
使用 count 查找 multimap 中某个键的个数
#include <iostream>
#include <map>
using namespace std;
int main()
{
cout << "嗨客网(www.haicoder.net)\n" << endl;
multimap<int, string> multimap1{{1, "Mon"}, {2, "Thu"}, {1, "Wed"}};
int count1 = multimap1.count(1);
cout << "Count1 = " << count1 << endl;
int count2 = multimap1.count(2);
cout << "Count2 = " << count2 << endl;
int count3 = multimap1.count(3);
cout << "Count3 = " << count3 << endl;
return 0;
}
编译后,我们直接运行生成的二进制文件 a.out
,如下图所示:
我们使用了 count 函数,查找了键在 multimap 中的个数,如果存在,则返回个数,不存在,则返回 0。
我们要在 STL 中的 multimap 容器中查找某个键的个数,我们可以使用 count 函数,传入我们需要查找的键名即可。