In C++, std::find() searches a range from left to right for the first element equal to a value. It returns an iterator that points at the match.
If nothing matches, it returns the last iterator, which is usually v.end(). This is the algorithm version. The string member std::string::find() returns an index instead.
Syntax
Add #include <algorithm>, then call std::find(). Start with these two lines.
Always compare the result with the end iterator before you use it.
Library declarations
C++
template<class InputIt, class T>
InputIt find(InputIt first, InputIt last, const T& value);
Parameters
Parameter
Description
first, last
Required. The range to search.
value
Required. The value to look for. Elements are compared with ==.
Return Value
Return Value
Description
Iterator
Points at the first element equal to value.
last
Returned when the value is not found (usually v.end()).
C++ Examples
Example 1 — Find a number
Find 30 in a vector and print its index.
C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 30, 40};
auto it = std::find(v.begin(), v.end(), 30);
if (it != v.end()) {
std::cout << "Found 30 at index " << std::distance(v.begin(), it) << '\n';
}
return 0;
}
Output
Found 30 at index 2
How It Works
find() walks the vector from the front and stops at the first element equal to 30.
The result is an iterator, not a number.
std::distance(v.begin(), it) turns the iterator into the index 2.
Example 2 — Value missing
Handle a search that finds nothing.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 30, 40};
auto it = std::find(v.begin(), v.end(), 99);
if (it == v.end()) {
std::cout << "99 is not in the list\n";
}
return 0;
}
Output
99 is not in the list
How It Works
No element equals 99, so find() checks all four and gives up.
On failure it returns the last argument, which here is v.end().
Testing it == v.end() is the safe way to detect a missing value.
Example 3 — Split an email
Find one character in a string, then use the position.
C++
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string email = "[email protected]";
auto it = std::find(email.begin(), email.end(), '@');
if (it != email.end()) {
std::cout << "User part: " << std::string(email.begin(), it) << '\n';
}
return 0;
}
Output
User part: user
How It Works
A string is a range of characters, so find() can search it for '@'.
The iterator points at the @ sign.
std::string(email.begin(), it) builds a new string from the start up to, but not including, @.
Frequently Asked Questions
In C++, std::find() finds the first element in a range that equals a value. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the first matching element, or the last iterator if there is no match.
std::find() is a general algorithm that returns an iterator. std::string::find() is a member function that returns an index or std::string::npos.
Use std::find_if() and pass a lambda. For example, find the first number above 10.