-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathfind_minmax.cpp
34 lines (27 loc) · 873 Bytes
/
find_minmax.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
#include <flux.hpp>
#include "assert.hpp"
#include <string>
#include <vector>
int main()
{
struct Person {
std::string name;
int age;
};
std::vector<Person> people{
{"Alice", 44},
{"Bob", 63},
{"Chris", 29},
{"Dani", 29},
{"Eddy", 63}
};
// find_minmax() returns a pair of cursors which we can destructure
// Here we'll get the min and max of the people vector, according to age
auto [min, max] = flux::find_minmax(people, flux::proj(flux::cmp::compare, &Person::age));
// The "minimum" is Chris. Dani is the same age, but Chris appears earlier
// in the sequence
assert(flux::read_at(people, min).name == "Chris");
// The "maximum" is Eddy. Bob is the same age, but Eddy appears later in the
// sequence
assert(flux::read_at(people, max).name == "Eddy");
}