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

📅  最后修改于: 2021-05-30 07:41:03             🧑  作者: Mango

multiset :: count()函数是C++ STL中的内置函数,该函数在multiset容器中搜索特定元素,并返回该元素的出现次数。

句法:

multiset_name.count(val)

参数:函数接受单个参数val ,该参数指定要在多集容器中搜索的元素。

返回值:该函数返回与multiset容器中的val相等的元素计数。

下面的程序说明了multiset :: count()函数:

程序1:

C++
// C++ program to demonstrate the
// multiset::count() function
#include 
using namespace std;
int main()
{
 
    int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 };
 
    // initializes the set from an array
    multiset s(arr, arr + 9);
 
    cout << "15 occurs " << s.count(15)
         << " times in container";
 
    return 0;
}


C++
// C++ program to demonstrate the
// multiset::count() function
#include 
using namespace std;
int main()
{
 
    int arr[] = { 15, 10, 15, 11, 10, 18, 18, 18, 18 };
 
    // initializes the set from an array
    multiset s(arr, arr + 9);
 
    cout << "18 occurs " << s.count(18)
         << " times in container";
 
    return 0;
}


输出:
15 occurs 2 times in container



程式2:

C++

// C++ program to demonstrate the
// multiset::count() function
#include 
using namespace std;
int main()
{
 
    int arr[] = { 15, 10, 15, 11, 10, 18, 18, 18, 18 };
 
    // initializes the set from an array
    multiset s(arr, arr + 9);
 
    cout << "18 occurs " << s.count(18)
         << " times in container";
 
    return 0;
}
输出:
18 occurs 4 times in container



要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”