Home >>Cpp Programs >C++ Find
This is the function in C++ that is basically used to find the element that are in the given range of numbers. C++ find also used to return an iterator to the first element in the range [first,last] that basically compares the equal to val. In case there is no such element is found then the function returns the last.
C++ search generally returns an iterator back to the first element in the range [first,last] that basically compares equal to val. Last is returned by the function in case there is no such element is found.
The CPP find function basically uses the operator== in order to compare the individual elements to val.
Here is an example that will explain about the C++ find and the working of it, most important of all, it will let you understand the application aspect of it:
#include<bits/stdc++.h> int main () { std::vector<int> vect { 10, 11, 12, 13 }; //store the position using iterator of searches element std::vector<int>::iterator it; // Original Vector print std::cout << "Original vector :"; for (int i=0; i<vect.size(); i++) std::cout << " " << vect[i]; std::cout << "\n"; //Need to searche element 12 int search = 12; it = std::find (vect.begin(), vect.end(), search); if (it != vect.end()) { std::cout << "Element " << search <<" found at position : " ; std:: cout << it - vect.begin() + 1 << "\n" ; } else { std::cout << "Element not found.\n\n"; } return 0; }