unordered_set_name.count(element)
此函数接受单个参数element 。表示容器中是否存在需要检查的元素。
如果元素存在于容器中,则此函数返回1,否则返回0。
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<int> sampleSet;
// Inserting elements
sampleSet.insert(5);
sampleSet.insert(10);
sampleSet.insert(15);
sampleSet.insert(20);
sampleSet.insert(25);
// displaying all elements of sampleSet
cout << "sampleSet contains: ";
for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {
cout << *itr << " ";
}
// checking if element 20 is present in the set
if (sampleSet.count(20) == 1) {
cout << "\nElement 20 is present in the set";
}
else {
cout << "\nElement 20 is not present in the set";
}
return 0;
}