TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm find() function

C++ algorithm find() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Function find()

C++ Algorithm find() function specifies a value in the argument list, a search for that value is made in the range, the iterator starts the search from the first element and goes on to the last element, if the element is found in the range then it is returned otherwise the last element of the range is given.

Syntax

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

Parameter

first: It specifies the first element of the range.

last: It specifies the last element of the range.

value: It specifies the value which is being searched in the range.

Return value

The function returns an iterator to the first element of the range that is equal to the value. If no such element is found, then the function returns the last element.

Example 1

#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:

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

Example 2

#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:

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

Complexity

The function moves in a linear way, starting from the first element going towards the last one. For each element of the list value of 'pred' is checked. The search goes on until a mismatch for the 'pred' value is encountered.

Data races

Either all the objects in the specified range or some of them are accessed by the function.

Exceptions

The function throws an exception if any of the argument throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf