-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstd::find in C++
40 lines (32 loc) · 987 Bytes
/
std::find in C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// CPP program to illustrate
// std::find
// CPP program to illustrate
// std::find
#include<bits/stdc++.h>
int main ()
{
std::vector<int> vec { 10, 20, 30, 40 };
// Iterator used to store the position
// of searched element
std::vector<int>::iterator it;
// Print Original Vector
std::cout << "Original vector :";
for (int i=0; i<vec.size(); i++)
std::cout << " " << vec[i];
std::cout << "\n";
// Element to be searched
int ser = 30;
// std::find function call
it = std::find (vec.begin(), vec.end(), ser);
if (it != vec.end())
{
std::cout << "Element " << ser <<" found at position : " ;
std::cout << it - vec.begin() << " (counting from zero) \n" ;
}
else
std::cout << "Element not found.\n\n";
return 0;
}
Output:
Original vector : 10 20 30 40
Element 30 found at position : 2 (counting from zero)