📜  C++ STL中的multimap :: count()

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

multimap :: count是C++ STL中的内置函数,它返回键在multimap容器中的出现次数。

句法:

multimap_name.count(key)

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

返回值:该函数返回键在多图容器中存在的次数。

// C++ function for illustration
// multimap::count() function
#include 
using namespace std;
  
int main()
{
  
    // initialize container
    multimap mp;
  
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 2, 60 });
    mp.insert({ 2, 20 });
    mp.insert({ 1, 50 });
    mp.insert({ 4, 50 });
  
    // count the number of times
    // 1 is there in the multimap
    cout << "1 exists " << mp.count(1) 
         << " times in the multimap\n";
  
    // count the number of times
    // 2 is there in the multimap
    cout << "2 exists " << mp.count(2) 
         << " times in the multimap\n";
  
    return 0;
}
输出:
1 exists 2 times in the multimap
2 exists 3 times in the multimap
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”