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

  • 9281021542 2018-02-20
  • 632231541 2018-02-20
  • 9751491540 2018-02-20
  • 8333621539 2018-02-20
  • 995001538 2018-02-20
  • 6477441537 2018-02-20
  • 511981536 2018-02-19
  • 2072881535 2018-02-19
  • 6024461534 2018-02-19
  • 7594061533 2018-02-19
  • 9311341532 2018-02-19
  • 4928331531 2018-02-19
  • 38561530 2018-02-19
  • 7776611529 2018-02-19
  • 3038041528 2018-02-19
  • 9566341527 2018-02-19
  • 3936031526 2018-02-19
  • 9531111525 2018-02-19
  • 291211524 2018-02-19
  • 8962001523 2018-02-19