Find index of an element in an array in C++
This post provides an overview of available methods to find an index of the first occurrence of an element in the array in C++.
1. Naive solution
A simple solution is to write our own custom routine for finding the index of the first occurrence of an element. The idea is to perform a linear search on the given array for determining the index. This approach is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; int main() { int arr[] = { 6, 3, 5, 2, 8 }; int n = sizeof(arr)/sizeof(arr[0]); int elem = 2; int i = 0; while (i < n) { if (arr[i] == elem) { break; } i++; } if (i < n) { cout << "Element " << elem << " is present at index " << i << " in the given array"; } else { cout << "Element is not present in the given array"; } return 0; } |
Output:
Element 2 is present at index 3 in the given array
2. Using std::find algorithm
We can also use the std::find algorithm, which returns an iterator that points to the target value. It is defined in the <algorithm> header. To get the required index, apply pointer arithmetic, or make a call to std::distance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = { 6, 3, 5, 2, 8 }; int n = sizeof(arr)/sizeof(arr[0]); int elem = 2; auto itr = find(arr, arr + n, elem); if (itr != end(arr)) { cout << "Element " << elem << " is present at index " << distance(arr, itr) << " in the given array"; } else { cout << "Element is not present in the given array"; } return 0; } |
Output:
Element 2 is present at index 3 in the given array
3. Using std::find_if algorithm
Sometimes it is desired to search for an element that meets certain conditions in the array. For instance, find the index of the first 2-digit number in the array. The recommended approach is to use the std::find_if algorithm, which accepts a predicate to handle such cases.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <algorithm> using namespace std; struct comp { int elem; comp(int const &i): elem(i) {} bool operator()(int const &i) { return (i == elem); } }; int main() { int arr[] = { 6, 3, 5, 2, 8 }; int n = sizeof(arr)/sizeof(arr[0]); int elem = 2; auto itr = find_if(arr, arr + n, comp(elem)); if (itr != end(arr)) { cout << "Element " << elem << " is present at index " << distance(arr, itr) << " in the given array"; } else { cout << "Element is not present in the given array"; } return 0; } |
Output:
Element 2 is present at index 3 in the given array
That’s all about finding the index of an element in an array in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)