What is C++ Find Function?

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 elements in the given range of numbers.
  • It takes three parameters – first, last, and val.

Syntax of the find function

The general syntax of the find() function is:

InputIterator find (InputIterator first, InputIterator last, const T& val)
first Initial position of the input iterator.
last The 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.
no match If no match is found the function returns last.

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:

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;


int main () {

  vector<int> vect_f = { 0, 5, 10, 15, 20, 25, 30 };

  //Which element to find?
  int fnd = 15;

  auto iter = find(vect_f.begin(), vect_f.end(), fnd);

  if (iter != vect_f.end()){

    cout << "Element found: " << *iter << "\n";

    cout << "Element position from 0 = " <<iter - vect_f.begin() << "\n" ;

  }

  else

    cout << "Searched Element Does not exist! \n";

  return 0;

}

Output:

Element found: 15
Element position from 0 = 3
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.

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

int main () {

  vector<int> vect_f = { 0, 5, 10, 15, 20, 25, 30 };

  //Which element to find?

  int fnd = 35;

  auto iter = find(vect_f.begin(), vect_f.end(), fnd);

  if (iter != vect_f.end()){

    cout << "Element found: " << *iter << "\n";

    cout << "Element position from 0 = " <<iter - vect_f.begin() << "\n" ;

  }

  else{

    cout << "We Searched for element: " << fnd << "\n";

    cout << "Searched Element Does not exist! \n";

  }

  return 0;

}

Output:

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:

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;


int main () {


  vector<int> vect_f = { 0, 5, 10, 15, 15, 10, 5 };

  //Which element to find?
  int fnd = 10;

  auto iter = find(vect_f.begin(), vect_f.end(), fnd);

  if (iter != vect_f.end()){

    cout << "Element found: " << *iter << "\n";

    cout << "Element position from 0 = " <<iter - vect_f.begin() << "\n" ;

  }

  else{

    cout << "We Searched for element: " << fnd << "\n";

    cout << "Searched Element Does not exist! \n";

  }

  return 0;

}

Output:

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 use the find() function to search an element in the array.

#include <iostream>
#include <algorithm>

using namespace std;

int main() {

int intArr[] = { 1, 3, 5, 7, 9, 11, 13, 15 };

size_t ArrSize = sizeof(intArr) / sizeof(int);

int *end = intArr + ArrSize;


  //Which element to find?

  int fnd = 11;

int *result = std::find(intArr, end, fnd);


if (result != end) {
 cout << "Element " <<fnd <<" exists in the array \n\n";
}

else{
 cout << "Element " <<fnd <<"  does not exist! \n\n";
}

  return 0;

}

Output:

CPP-find-array

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!