温馨提示×

c++ set count怎样快速统计元素

c++
小樊
129
2024-07-12 14:57:27
栏目: 编程语言

你可以使用std::set中的count函数来统计元素的数量。count函数接受一个元素作为参数,并返回set中与该元素相等的元素的数量。

示例代码如下:

#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {1, 2, 3, 4, 4, 5};

    int element = 4;
    int count = mySet.count(element);

    std::cout << "Element " << element << " appears " << count << " times in the set." << std::endl;

    return 0;
}

在这个例子中,我们创建了一个包含重复元素的set,然后使用count函数统计特定元素的数量。

0