-
Notifications
You must be signed in to change notification settings - Fork 7
/
40_set_algos.cpp
71 lines (61 loc) · 1.34 KB
/
40_set_algos.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
68
69
70
71
/* output
set s1: 1 2 3 4 5 7
set s2: 1 2 3 4 6 8
union: 1 2 3 4 5 6 7 8
intersection: 1 2 3 4
difference(s1,s2): 5 7
difference(s2,s1): 6 8
symmetric_difference: 5 6 7 8
*/
#include <range/v3/algorithm/set_algorithm.hpp>
namespace rng = ranges::v3; //easy access
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
void output(const vector<int>& range) {
for (auto i : range) {
cout << i << ' ';
}
cout << endl;
}
int main() {
vector<int> s1 = {1, 2, 3, 4, 5, 7};
cout << "set s1: ";
output(s1);
vector<int> s2 = {1, 2, 3, 4, 6, 8};
cout << "set s2: ";
output(s2);
{
vector<int> u;
rng::set_union(s1, s2, rng::back_inserter(u));
cout << "union: ";
output(u);
}
{
vector<int> u;
rng::set_intersection(s1, s2, rng::back_inserter(u));
cout << "intersection: ";
output(u);
}
{
vector<int> u;
rng::set_difference(s1, s2, rng::back_inserter(u));
cout << "difference(s1,s2): ";
output(u);
}
{
vector<int> u;
//an example of the interator based call
rng::set_difference(
s2.cbegin(), s2.cend(), s1.cbegin(), s1.cend(), rng::back_inserter(u));
cout << "difference(s2,s1): ";
output(u);
}
{
vector<int> u;
rng::set_symmetric_difference(s2, s1, rng::back_inserter(u));
cout << "symmetric_difference: ";
output(u);
}
}