当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C++ unordered_set find()用法及代码示例

unordered_set::find()函数是C++ STL中的内置函数,用于在容器中搜索元素。它返回元素的迭代器,如果找到其他元素,则返回指向unordered_set::end()的迭代器。

用法

unordered_set_name.find(key)

参数:此函数接受必需的参数键,该键指定要搜索的元素。


返回值:返回找到元素的迭代器,否则返回指向unordered_set末尾的迭代器。

以下示例程序旨在说明unordered_set::find()函数:

示例1:

// C++ program to illustrate the 
// unordered_set::find() function 
  
#include <iostream> 
#include <string> 
#include <unordered_set> 
  
using namespace std; 
  
int main() 
{ 
  
    unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" }; 
  
    // use of find() function 
    if (sampleSet.find("geeks1") != sampleSet.end()) { 
        cout << "element found." << endl; 
    } 
    else { 
        cout << "element not found" << endl; 
    } 
  
    return 0; 
}
输出:
element found.

示例2:

// CPP program to illustrate the 
// unordered_set::find() function 
  
#include <iostream> 
#include <string> 
#include <unordered_set> 
  
using namespace std; 
  
int main() 
{ 
  
    unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" }; 
  
    // use of find() function 
    if (sampleSet.find("geeksforgeeks") != sampleSet.end()) { 
        cout << "found" << endl; 
    } 
    else { 
        cout << "Not found" << endl; 
    } 
  
    return 0; 
}
输出:
Not found


相关用法


注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 unordered_set find() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。