forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.cpp
More file actions
57 lines (45 loc) · 1.18 KB
/
find.cpp
File metadata and controls
57 lines (45 loc) · 1.18 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*******************************************************************************
*
* Program: find Function()
*
* Description: Examples of using the find() function in C++ to find the first
* occurrence of a value in a range (or the first equal object/value according
* to the equality == operator).
*
* YouTube Lesson: https://www.youtube.com/watch?v=9uy1tBnAAhU
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Number
{
public:
int n;
Number(int n) : n(n) {}
bool operator==(const Number &num)
{
return (this->n == num.n);
}
};
int main()
{
vector<int> data = {9,8,7,5,4,3,2};
auto itfound = find(data.begin(), data.end(), 6);
if (itfound == data.end())
{
cout << "6 not found in range" << endl;
}
else
{
cout << *itfound << endl;
cout << "Index: " << itfound - data.begin() << endl;
}
vector<Number> datan = {Number(1), Number(2), Number(3)};
auto itfoundn = find(datan.begin(), datan.end(), Number(2));
cout << (*itfoundn).n << endl;
return 0;
}