xxxxxxxxxx
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = { 7, 3, 6, 2, 6 };
int key = 6;
vector<int>::iterator itr = find(v.begin(), v.end(), key);
int index = distance(v.begin(), itr);
cout << index << endl;
return 0;
}
find index in vector c++
xxxxxxxxxx
// C++ program to find the index
// of an element in a vector
#include <bits/stdc++.h>
using namespace std;
// Function to print the
// index of an element
void getIndex(vector<int> v, int K)
{
auto it = find(v.begin(), v.end(), K);
// If element was found
if (it != v.end())
{
// calculating the index
// of K
int index = it - v.begin();
cout << index << endl;
}
else {
// If the element is not
// present in the vector
cout << "-1" << endl;
}
}
// Driver Code
int main()
{
// Vector
vector<int> v = { 1, 45, 54, 71, 76, 17 };
// Value whose index
// needs to be found
int K = 54;
getIndex(v, K);
return 0;
}