Algorithm Find Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Find Function

Algorithm Find Function

BLUF: Mastering Algorithm Find Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Find Function

C++ is renowned for its efficiency. Learn how Algorithm Find Function enables low-level control and high-performance computing in the tutorial below.

The find function in C++ algorithm requires a specific value in the argument list to perform a search within a given range. The search begins from the first element in the range and iterates through to the last element. If the desired element is located within the range, it is returned; otherwise, the last element of the range is returned.

Syntax

Example

template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& value);

Parameter

It designates the initial element within the range.

The final value indicates the concluding element within the range.

The value parameter indicates the specific value that is being searched within the specified range.

Return value

The function provides an iterator pointing to the initial element within the range that matches the specified value. In cases where no matching element is present, the function returns a pointer to the final element.

Example 1

Example

#include <iostream>     
#include <algorithm>    
#include <vector>       
int main ()
{ 
  int newints[] = { 50, 60, 70, 80 };
  int * q;
 q = std::find (newints, newints+4, 60);
 if (q != newints+4)
 std::cout << "Element found in newints: " << *q << '\n';
  else
    std::cout << "Element not found in newints\n";
  std::vector<int> newvector (newints,newints+4);
  std::vector<int>::iterator ti;
  ti = find (newvector.begin(), newvector.end(), 60);
  if (ti != newvector.end())
   std::cout << "Element found in newvector: " << *ti << '\n';
  else
    std::cout << "Element not found in newvector\n";
 return 0;
}

Output:

Output

Elements that are found in newints: 60
Elements that are found in newvector: 60

Example 2

Example

#include<iostream>
#include<algorithm>
#include<vector>
int main()
{
	std:: vector<int> vct {50,60,70,80}; 
	std::vector<int>::iterator ti;
	std::cout<<"Initial vector:";
	for(int k=0; k<vct.size(); k++)
	std::cout<<" "<<vct[k];
    	std::cout<<"\n";
	int sr = 60;
	
	ti = std::find(vct.begin(), vct.end(),sr);
	if(ti!=vct.end())
	{
		std::cout<< "The element "<<"has been found at position:";
		std::cout<<ti-vct.begin() +1<<"\n";
	}
	else
		std::cout<<"Element does not exist.\n \n";
	return 0;
}

Output:

Output

Intitial vector: 50 60 70 80
The element 30 has been found  at position: 2

Complexity

The process progresses sequentially, initiating from the initial item and advancing towards the final one. Every item in the list is assessed against the 'pred' value. The iteration continues until a discrepancy in the 'pred' value is detected.

Data races

The function accesses either all the objects within the specified range or a subset of them.

Exceptions

The function will raise an exception if any of its arguments also raises an exception.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience