Skip to content
C++ Better Explained
Go back
How to Find an Element in a Vector in C++ (std::find)
Edit page

How to Find an Element in a Vector

Searching a vector is something you’ll do constantly — checking if a value is present, finding where it sits, or locating the first item that matches a rule. C++ gives you std::find and std::find_if for exactly this. Here’s how to use them.


The Basics: std::find

std::find lives in the <algorithm> header. You give it the start and end of the vector plus the value you want, and it hands back an iterator:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> nums = {3, 9, 14, 27, 5};

    auto it = std::find(nums.begin(), nums.end(), 14);
    if (it != nums.end())
        std::cout << "Found it!\n";
    else
        std::cout << "Not here.\n";
    return 0;
}

The key is the test it != nums.end(). The special “end” iterator means “past the last element,” so getting it back means the value wasn’t found. Always check against end() before using the result.


Getting the Index

An iterator tells you where the match is, but often you want a plain number. Convert it with std::distance:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> nums = {3, 9, 14, 27, 5};
    auto it = std::find(nums.begin(), nums.end(), 27);
    if (it != nums.end()) {
        int index = std::distance(nums.begin(), it);
        std::cout << "27 is at index " << index << "\n";  // 3
    }
    return 0;
}

std::distance measures how far the match is from the beginning, which is exactly the zero-based index. Note the check is still there — never call distance on an iterator you haven’t confirmed.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Searching by a Condition: find_if

Sometimes you don’t have an exact value — you want “the first number over 10” or “the first empty name.” That’s what std::find_if is for. You pass it a small function (a lambda) that returns true for a match:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> nums = {3, 9, 14, 27, 5};
    auto it = std::find_if(nums.begin(), nums.end(),
                           [](int x){ return x > 10; });
    if (it != nums.end())
        std::cout << "First number over 10: " << *it << "\n";  // 14
    return 0;
}

The lambda [](int x){ return x > 10; } is called on each element until one returns true. find_if then stops and returns that element. This is far cleaner than writing your own loop with a flag.


Just Checking Existence

If you only care whether something is present — not where — compare find to end() right in a bool:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main() {
    std::vector<std::string> names = {"Ann", "Bob", "Cy"};
    bool hasBob = std::find(names.begin(), names.end(), "Bob") != names.end();
    std::cout << (hasBob ? "Bob is here\n" : "No Bob\n");
    return 0;
}

This reads almost like a sentence: “does names contain Bob?” It works for strings, ints, and any type that supports ==.


A Note on Speed

std::find checks elements one by one, so on a vector of n items it may look at all n — fine for small or unsorted data. If you search a large collection constantly, a std::set or std::unordered_map can look values up far faster. For everyday vector searches, though, std::find is the right, simple choice.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ Exponents: Why ^ Isn't Power and How to Use pow()
Next Post
C++ Modulo Operator (%): Remainders Explained for Beginners