This tutorial will discuss how to find an element in vector in C++.

To find an element in a vector, we can use the STL algorithm find(). It accepts three arguments: the first is an iterator pointing to the start of the sequence, the second is an iterator pointing to the end of the sequence, and the third is the value you’re searching for.

auto it = std::find(start_iterator, end_iterator, value);

The find() function iterates over all the elements between the start and end iterators provided as the first two arguments, searching for the given value. If the value exists, it will return an iterator pointing to that element within the specified range. However, it searches for the first occurrence only; as soon as the element is found, it returns the iterator pointing to that value. If the element doesn’t exist, it returns the iterator pointing to the end of the range.

To find an element in a vector, pass the iterator returned by the vector’s begin function as the first argument to find(), and the iterator returned by the end function (which is the iterator just past the last element) as the second argument. The third argument is the value you’re searching for.

std::vector<int> vecObj = {12, 45, 33, 90, 81};

int value = 33;

// Find the first occurrence of value in the vector.
auto it = std::find(vecObj.begin(), vecObj.end(), value);

If the find function returns a valid iterator (not equal to the end iterator), it means the value exists in the vector. To determine the index position of that value, we can use the STL distance function. Pass the returned iterator from find and the iterator pointing to the first element of the vector (from the begin function) as arguments to distance. This will return the number of elements between these two iterators, essentially the index position of the found element in the vector.

// Check if iterator is valid
if (it != vecObj.end())
{
    // Get the index position at which element exist
    auto pos = std::distance(vecObj.begin(), it);
    std::cout << "Element " << value << " found at Index Position: " << pos << std::endl;
}
else
{
    std::cout << "Element " << value << " not found!" << std::endl;
}

However, if the iterator returned by the find function is equal to the iterator returned by the vector’s end function, it indicates that the value doesn’t exist in the vector.

Let’s see the complete example,

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

int main()
{
    std::vector<int> vecObj = {12, 45, 33, 90, 81};

    int value = 33;

    // Find the first occurrence of value in the vector.
    auto it = std::find(vecObj.begin(), vecObj.end(), value);

    // Check if iterator is valid
    if (it != vecObj.end())
    {
        // Get the index position at which element exist
        auto pos = std::distance(vecObj.begin(), it);
        std::cout << "Element " << value << " found at Index Position: " << pos << std::endl;
    }
    else
    {
        std::cout << "Element " << value << " not found!" << std::endl;
    }

    return 0;
}

Output

Element 33 found at Index Position: 2

Summary

Today, we learned how to find an element in vector in C++.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.