forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupby_examples.cpp
More file actions
48 lines (42 loc) · 1.25 KB
/
Copy pathgroupby_examples.cpp
File metadata and controls
48 lines (42 loc) · 1.25 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
#include <groupby.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <functional>
int main() {
auto len = [](const std::string& str){ return str.size(); };
std::vector<std::string> vec = {
"hi", "ab", "ho",
"abc", "def",
"abcde", "efghi"
};
std::cout << "strings grouped by their length\n";
for (auto&& gb : iter::groupby(vec, len)) {
std::cout << "key(" << gb.first << "): ";
for (auto&& s : gb.second) {
std::cout << s << " ";
}
std::cout << '\n';
}
// groups may be skipped entirely or partially consumed
std::cout << "skipping length of 3\n";
for (auto&& gb : iter::groupby(vec, len)) {
if (gb.first == 3) {
continue;
}
std::cout << "key(" << gb.first << "): ";
for (auto&& s : gb.second) {
std::cout << s << " ";
}
std::cout << '\n';
}
std::cout << "ints grouped by their value:\n";
std::vector<int> ivec = {5, 5, 6, 6, 19, 19, 19, 19, 69, 0, 10, 10};
for (auto&& gb : iter::groupby(ivec)) {
std::cout << "key(" << gb.first << "): ";
for (auto&& s : gb.second) {
std::cout << s << " ";
}
std::cout << '\n';
}
}