📜  C++ STL中的unordered_multiset count()函数

📅  最后修改于: 2021-05-30 12:32:06             🧑  作者: Mango

unordered_multiset :: count()是C++ STL中的内置函数,它返回unordered_multiset容器中等于给定值的元素计数。

句法:

unordered_multiset_name.count(val)

参数:该函数接受单个强制参数val ,该参数指定要返回其元素的计数(在unordered_multiset容器中)的元素。

返回值:返回一个无符号整数类型,该整数类型表示容器中某个出现的次数。

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multiset::count() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert(11);
    sample.insert(11);
    sample.insert(11);
    sample.insert(12);
    sample.insert(13);
    sample.insert(13);
    sample.insert(14);
  
    cout << "11 occurs " << sample.count(11) << " times";
    cout << "\n12 occurs " << sample.count(13) << " times";
    cout << "\n13 occurs " << sample.count(13) << " times";
    cout << "\n14 occurs " << sample.count(14) << " times";
  
    return 0;
}
输出:
11 occurs 3 times
12 occurs 2 times
13 occurs 2 times
14 occurs 1 times

程式2:

// C++ program to illustrate the
// unordered_multiset::count() function
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('a');
    sample.insert('a');
    sample.insert('b');
    sample.insert('b');
    sample.insert('c');
    sample.insert('c');
  
    cout << "a occurs " << sample.count('a') << " times";
    cout << "\nb occurs " << sample.count('b') << " times";
    cout << "\nc occurs " << sample.count('c') << " times";
  
    return 0;
}
输出:
a occurs 3 times
b occurs 2 times
c occurs 2 times
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”