-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathstl_08_adjacent_find.cpp
67 lines (50 loc) · 1.27 KB
/
stl_08_adjacent_find.cpp
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
58
59
60
61
62
63
64
65
66
67
// code_report
// https://youtu.be/UN_twk5w0RM
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
void example1 ()
{
vector<int> v = { 0, 5, 2, 2, 3, 1, 4, 4 };
auto it = adjacent_find (v.begin (), v.end ());
cout << "Value: " << *it << endl;
cout << "Index: " << distance (v.begin (), it) << endl;
}
void example2 ()
{
vector<int> v = { 0, 5, 2, 2, 3, 1, 4, 4 };
auto it = adjacent_find (v.begin (), v.end (), greater<> ());
cout << "Value: " << *it << endl;
cout << "Index: " << distance (v.begin (), it) << endl;
}
void example3 ()
{
vector<int> v = { 0, 5, 2, 2, 3, 1, 4, 4 };
auto it = adjacent_find (v.begin (), v.end (), less<> ());
cout << "Value: " << *it << endl;
cout << "Index: " << distance (v.begin (), it) << endl;
}
template <typename FwdIt>
auto adjacent_count (FwdIt first, FwdIt last) {
auto c = 0;
while (true) {
first = adjacent_find (first, last);
if (first == last) return c;
++c, ++first;
}
return c;
}
void example4 ()
{
vector<int> v = { 0, 5, 2, 2, 3, 1, 4, 4 };
cout << "Count: " << adjacent_count (v.begin (), v.end ()) << endl;
}
int main () {
example1 ();
example2 ();
example3 ();
example4 ();
return 0;
}