Hit enter after type your search item

What is C++ Find function? [4 Programs to Understand]

What is find() function in C++?

  • The find() function can be used in C++ programs by including <algorithm.h> in the header.
  • The find function is used to search element in the given range of numbers.
  • It takes three parameters – first, last, val
  • first: Initial position of the input iterator
  • last: Last position of the input iterator
  • val: A value to be provided that is compared with the elements
  • Returns: If the val provided is found then the find() function returns the iterator pointing to the first occurrence of the element
  • If no match is found the function returns last

Syntax of the find function

The general syntax of find function is:

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

Now let us look at a few C++ programs to understand how find() function works in C++ with different ranges.

An example of find() with vector

For this program, we have a vector with seven elements of int type. Then we used the find function to search for an element by providing the first and last parameters along with a value.

Have a look at the code and output:



In the above program:

  • We searched for element 15 in the vector
  • It exists at position 3 – as we count from zero

What happens if elements is not found?

In this program, we searched for an element that does not exist in the vector’s given range.


We Searched for element: 15

Searched Element Does not exist!

What if duplicate elements exist in the range?

This time we have a vector with duplicate elements. We will search element 10 which exists twice in the vector, see what output we get:


Element found: 10

Element position from 0 = 2

So, the find() function returns the occurrence of the first element found in the given range.

Using find function with an array example

The following C++ program uses an array and then we used the find() function to search an element in the array.




This div height required for enabling the sticky sidebar