Find Algorithm STL C++
The find algorithm stl in C++ searches the range start to end for the value specified by val.It returns an iterator to the first occurrence of the element or to end if the value is not in the sequence.
Syntax of find algorithm
template< class InputIterator, class T > InputIterator find( InputIterator first, InputIterator last, const T& value );
Find Algorithm implementation C++
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef int type; void print_vector(vector <type> vec0) { vector <type>::iterator it; for ( it = vec0.begin( ) ; it != vec0.end( ) ; it++ ) cout << *it << " "; cout<< endl; }// print int main( ) { vector <type> vec0; for ( int i = 0 ; i <= 10 ; i++ ) vec0.push_back(i*10 ); cout<< "vector : "<<endl; print_vector(vec0); fill_n (vec0.begin(),3,-1); fill_n (vec0.begin()+4,5,0); cout<< "vector after the replace "<<endl; print_vector(vec0); }//edn of main
Program Output
Searching for 3 in the set: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15