C++ std::find() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

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.

C++
std::find(v.begin(), v.end(), 30);
std::find(text.begin(), text.end(), '@');
CallWhat it does
std::find(v.begin(), v.end(), 30);Search the whole vector for the value 30.
std::find(text.begin(), text.end(), '@');Search a string for one character.

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

ParameterDescription
first, lastRequired. The range to search.
valueRequired. The value to look for. Elements are compared with ==.

Return Value

Return ValueDescription
IteratorPoints at the first element equal to value.
lastReturned 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;
}

How It Works

  1. find() walks the vector from the front and stops at the first element equal to 30.
  2. The result is an iterator, not a number.
  3. 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;
}

How It Works

  1. No element equals 99, so find() checks all four and gives up.
  2. On failure it returns the last argument, which here is v.end().
  3. 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;
}

How It Works

  1. A string is a range of characters, so find() can search it for '@'.
  2. The iterator points at the @ sign.
  3. 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.

Next: find_end()

Finds the last occurrence of a sequence inside a range.

find_end() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful