Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

std::find in C++

  • Difficulty Level : Easy
  • Last Updated : 17 Feb, 2020

Finds the element in the given range of numbers. Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.

Function Template :

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

InputIterator find (InputIterator first, InputIterator last, const T& val)

first,last :
Input iterators to the initial and final positions in a sequence. The range
searched is [first,last), which contains all the elements between first and
last, including the element pointed by first but not the element pointed by last.

val :
Value to be search in the range

Return Value :
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.


Input : 10 20 30 40
Output : Element 30 found at position : 2 (counting from zero)  

Input : 8 5 9 2 7 1 3 10
Output : Element 4 not found.    

// CPP program to illustrate 
// std::find
// CPP program to illustrate 
// std::find
int main ()
    std::vector<int> vec { 10, 20, 30, 40 };
    // Iterator used to store the position 
    // of searched element
    std::vector<int>::iterator it;
    // Print Original Vector
    std::cout << "Original vector :";
    for (int i=0; i<vec.size(); i++)
        std::cout << " " << vec[i];
    std::cout << "\n";
    // Element to be searched
    int ser = 30;
    // std::find function call
    it = std::find (vec.begin(), vec.end(), ser);
    if (it != vec.end())
        std::cout << "Element " << ser <<" found at position : " ;
        std::cout << it - vec.begin() << " (counting from zero) \n" ;
        std::cout << "Element not found.\n\n";
    return 0;


Original vector : 10 20 30 40
Element 30 found at position : 2 (counting from zero)

Related Articles:

This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :