STL unordered_multiset元素个数

STL unordered_multiset元素个数教程

我们要在 STL 中的 unordered_multiset 容器中查找某个元素的个数,我们可以使用 count 函数,传入我们需要查找的元素名即可。

STL unordered_multiset count详解

语法

count(val)

参数

参数 描述
val 需要查找的元素。

说明

返回元素 val 在 unordered_multiset 中的个数。

案例

使用count查找元素个数

使用 count 查找 unordered_multiset 中某个元素的个数

#include <iostream> #include <unordered_set> using namespace std; int main() { cout << "嗨客网(www.haicoder.net)\n" << endl; unordered_multiset<string> unordered_multiset1{"haicoder", "haicoder.net", "haicoder"}; int count1 = unordered_multiset1.count("haicoder"); cout << "Count1 = " << count1 << endl; int count2 = unordered_multiset1.count("haicoder.net"); cout << "Count2 = " << count2 << endl; int count3 = unordered_multiset1.count("HaiCoder"); cout << "Count3 = " << count3 << endl; return 0; }

编译后,我们直接运行生成的二进制文件 a.out,如下图所示:

54_unordered_multiset元素个数.png

我们使用了 count 函数,查找了元素在 unordered_multiset 中的个数,如果存在,则返回个数,不存在,则返回 0。

STL unordered_multiset元素个数总结

我们要在 STL 中的 unordered_multiset 容器中查找某个元素的个数,我们可以使用 count 函数,传入我们需要查找的元素名即可。