set::count()
是c stl中的内置函数,它返回元素在集合中出现的次数。由于set容器仅包含唯一元素,因此只能返回1或0。
- 用法:
set_name.count(element)
- 参数:该函数接受一个强制性参数
element
,该元素指定要返回其计数的元素。 - 返回值:该函数返回1或0,因为该集合仅包含唯一元素。如果设置的容器中存在该值,则返回1。如果容器中不存在它,则返回0。
- 实例:
#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