C++ unordered_set find() Function
Example
Find "Ford" in an unordered set:
unordered_set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
auto it = cars.find("Ford");
if (it != cars.end()) {
cout << "Found " << *it;
} else {
cout << "Not found";
}
Definition and Usage
The find() function searches for an element with the given value, and returns an iterator (a pointer-like object that points to an element) to it.
If the value does not exist, find() returns end(). Always compare the result with end() before you use it.
The find function is defined in the <unordered_set> header file.
Syntax
unordered_set.find(type value)
type is the type of the elements in the unordered set, like string in the example.
Parameter Values
| Parameter | Description |
|---|---|
| value | Required. The value to search for. |
Technical Details
| Returns: | An iterator pointing to the element with the given value. If the value does not exist, it returns unordered_set.end(). |
|---|
Related Pages
Read more about unordered sets in our Unordered Set Tutorial.