Given a vector V consisting of N integers and an element K, the task is to find the index of element K in the vector V. If the element does not exist in vector then print 1.
Examples:
Input: V = {1, 45, 54, 71, 76, 17}, K = 54
Output: 2
Explaination :
The index of 54 is 2, hence output is 2.Input: V = {3, 7, 9, 11, 13}, K = 12
Output: 1
Approach:
Follow the steps below to solve the problem:

 Index of an element in a vector can be found with the help of the two predefined functions in stl:
 find(): Used to find the position of element in the vector.
 distance(): Used to find distance between base iterater of vector and iterator return by find() function.
 Finaly return the distance returned by distance() function.
Below is the implementation of the above approach :
C++
// 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 = distance(v.begin(), it); 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; } 
2
Time Complexity : O (N)
Auxiliary Space : O (1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
 How to find the maximum element of a Vector using STL in C++?
 How to find the minimum and maximum element of a Vector using STL in C++?
 Program to find frequency of each element in a vector using map in C++
 Last element of vector in C++ (Accessing and updating)
 Delete array element in given index range [L  R]
 How to find the sum of elements of a Vector using STL in C++?
 How to find common elements between two Vector using STL in C++?
 Find and print duplicate words in std::vector<string> using STL functions
 How to erase an element from a vector using erase() and reverse_iterator?
 vector :: cbegin() and vector :: cend() in C++ STL
 vector::push_back() and vector::pop_back() in C++ STL
 vector::crend() & vector::crbegin() with example
 vector::front() and vector::back() in C++ STL
 vector::empty() and vector::size() in C++ STL
 How to flatten a Vector of Vectors or 2D Vector in C++
 Number of permutations such that sum of elements at odd index and even index are equal
 vector::at() and vector::swap() in C++ STL
 vector::begin() and vector::end() in C++ STL
 Find the character in first string that is present at minimum index in second string
 How to find the maximum element of an Array using STL in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.