find(): This method is used to find the first occurrence of a value in a sequence.
If the element is present in the sequence then it returns the iterator to the first occurrences of the given element, else it points to the end of the sequence.
This is available in the file: stl_algo.h
Syntax:
find(first_iter, last_iter, value)
first_iter: It is the address of the start( base address) of the sequence. For e.g. in vector, vec it will be
vec.begin().
last_iter: It is the address of the end of the sequence. For e.g. in vector, vec it will be
vec.end().
value: It is the value to search in the sequence.
Example:
Input: arr = {1, 2, 3, 3, 5}, value = 3
Output: Element is founded first at 2
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){vector<int> arr = {1, 2, 3, 3, 5};int value = 3;auto it = find(arr.begin(), arr.end(), value);if (it == arr.end())cout << "Element is not founded in the array\n";elsecout << "Element is founded first at " <<it - arr.begin() << "\n";return 0;}
No comments:
Post a Comment