C++:unordered_set的count

C++:unordered_set的count

用法

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; 
}

猜你喜欢

转载自blog.csdn.net/qq_44861043/article/details/120788936