📜  在C++ STL中设置count()函数

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

set :: count()是C++ STL中的内置函数,它返回元素在集合中出现的次数。它只能返回1或0,因为set容器仅包含唯一元素。

句法:

set_name.count(element) 

参数:函数接受一个强制性参数元素,该元素指定要返回其计数的元素。

返回值:该函数返回1或0,因为该集合仅包含唯一元素。如果设置的容器中存在该值,则返回1。如果容器中不存在它,则返回0。

下面是上述函数的说明。

// CPP program to demonstrate the
// set::count() function
#include 
using namespace std;
int main()
{
  
    int arr[] = { 14, 12, 15, 11, 10 };
  
    // initializes the set from an array
    set s(arr, arr + 5);
  
    // check if 11 is present or not
    if (s.count(11))
        cout << "11 is present in the set\n";
    else
        cout << "11 is not present in the set\n";
  
    // checks if 18 is present or not
    if (s.count(18))
        cout << "18 is present in the set\n";
    else
        cout << "18 is not present in the set\n";
  
    return 0;
}
输出:
11 is present in the set
18 is not present in the set
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”