📌  相关文章
📜  C++ STL中的unordered_multimap count()函数

📅  最后修改于: 2021-05-30 16:33:44             🧑  作者: Mango

unordered_multimap :: count()是C++ STL中的内置函数,该函数返回容器中其键等于在参数中传递的键的元素数。

句法:

unordered_multimap_name.count(key)

参数:该函数接受单个强制性参数,该键指定要返回其在unordered_multimap容器中的计数的键。

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

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

程序1:

// C++ program to illustrate the
// unordered_multimap::count()
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts key and element
    sample.insert({ 10, 100 });
    sample.insert({ 10, 100 });
    sample.insert({ 20, 200 });
    sample.insert({ 30, 300 });
    sample.insert({ 30, 150 });
  
    cout << "10 occurs " << sample.count(10) 
         << " times";
  
    cout << "\n20 occurs " << sample.count(20) 
         << " times";
  
    cout << "\n13 occurs " << sample.count(13) 
         << " times";
  
    cout << "\n30 occurs " << sample.count(30) 
         << " times";
  
    return 0;
}
输出:
10 occurs 2 times
20 occurs 1 times
13 occurs 0 times
30 occurs 2 times

程式2:

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