C++ set find() Function
Example
Find "Ford" in a set:
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 <set> header file.
Syntax
set.find(type value)
type is the type of the elements in the 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 set.end(). |
|---|
Related Pages
Read more about sets in our Sets Tutorial.