find
描述 (Description)
它用于搜索元素的容器,其中k为value,如果找到则返回迭代器,否则返回itoror到unordered_set :: end。
声明 (Declaration)
以下是std :: unordered_set :: find的声明。
C++11
iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;
参数 (Parameters)
k - K是搜索元素。
返回值
如果找到指定的值,它返回元素的迭代器,如果在容器中找不到,则返回unordered_set :: end。
异常 (Exceptions)
如果任何元素比较对象抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂
恒定时间。
例子 (Example)
以下示例显示了std :: unordered_set :: find的用法。
#include <iostream>
#include <string>
#include <unordered_set>
int main () {
std::unordered_set<std::string> myset = { "sai","ram,","krishna" };
std::string input;
std::cout << "Enter the myset char: ";
getline (std::cin,input);
std::unordered_set<std::string>::const_iterator got = myset.find (input);
if ( got == myset.end() )
std::cout << "not found in myset";
else
std::cout << *got << " is in myset";
std::cout << std::endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Enter the myset char: krishna
krishna is in myset