Find Element Index in Vector in C++

  1. Use Custom Function to Find Element Index in Vector in C++
  2. Use std::find Algorithm to Find Element Index in Vector in C++
  3. Use std::find_if Algorithm to Find Element Index in Vector in C++

This article will explain several methods of how to find element index in vector in C++.

Use Custom Function to Find Element Index in Vector in C++

We can use a custom linear search function to find the given element’s position in the vector. Note that this is a pretty inefficient method to solve this problem. In the following example code, we declare a vector of integers and look for an arbitrary element position, which we output if the call is successful.

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

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;

int findIndex(const vector<int> &arr, int item) {

    for (auto i = 0; i < arr.size(); ++i) {
        if (arr[i] == item)
            return i;
    }

    return -1;
}

int main(int argc, char *argv[]) {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    auto pos = findIndex(arr, 32);
    pos != -1 ?
    cout << "Found the element " << 32 << " at position " << pos << endl :
    cout << "Could not found the element " << 32 << " in vector" << endl;

    exit(EXIT_SUCCESS);
}

Output:

Could not found the element 32 in vector

Use std::find Algorithm to Find Element Index in Vector in C++

Alternatively, we may use the std::find algorithm that’s part of the STL library. This function returns the iterator to the first element that satisfies the condition. On the other hand, if no element is found, the algorithm returns the last element of the range. Mind though, returned iterator should be decremented by begin iterator to calculate the position.

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

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;

int findIndex2(const vector<int> &arr, int item) {
    auto ret = std::find(arr.begin(), arr.end(), item);

    if (ret != arr.end())
        return ret - arr.begin();
    return -1;
}

int main(int argc, char *argv[]) {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    auto pos2 = findIndex2(arr, 10);
    pos2 != -1 ?
    cout << "Found the element " << 10 << " at position " << pos2 << endl :
    cout << "Could not found the element " << 10 << " in vector" << endl;

    exit(EXIT_SUCCESS);
}

Output:

Found the element 10 at position 9

Use std::find_if Algorithm to Find Element Index in Vector in C++

Another method to find the index of the element is to invoke the std::find_if algorithm. It’s similar to the std::find except that the third argument can be a predicate expression to evaluate each iterated element. If the expression returns true, then the algorithm will return. Notice that we pass the lambda expression as the third argument, which checks if the element is equal to 10.

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

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;

int main(int argc, char *argv[]) {
    vector<int> arr = {1,2,3,4,5,6,7,8,9,10};

    auto ret = std::find_if(arr.begin(), arr.end(),
                            [](int x) { return x == 10; });

    if (ret != arr.end())
        cout << "Found the element " << 10 << " at position "
             << ret - arr.begin() << endl;

    exit(EXIT_SUCCESS);
}

Output:

Found the element 10 at position 9
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Vector

  • Iterate Though a Vector in C++
  • Sort Vector in C++