public member function
<unordered_set>

std::unordered_set::find

      iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;
Get iterator to element
Searches the container for an element with k as value and returns an iterator to it if found, otherwise it returns an iterator to unordered_set::end (the element past the end of the container).

Another member function, unordered_set::count, can be used to just check whether a particular element exists.

All iterators in an unordered_set have const access to the elements (even those whose type is not prefixed with const_): Elements can be inserted or removed, but not modified while in the container.

Parameters

k
Key to be searched for.
Member type key_type is the type of the elements in the container. In unordered_set containers it is the same as value_type, defined as an alias of the class's first template parameter (Key).

Return value

An iterator to the element, if the specified value is found, or unordered_set::end if it is not found in the container.

Member types iterator and const_iterator are forward iterator types. Both may be aliases of the same iterator type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// unordered_set::find
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_set<std::string> myset = { "red","green","blue" };

  std::string input;
  std::cout << "color? ";
  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;
}


Possible output:
color? blue
blue is in myset

Complexity

Average case: constant.
Worst case: linear in container size.

Iterator validity

No changes.

See also