forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted_examples.cpp
More file actions
33 lines (29 loc) · 1011 Bytes
/
sorted_examples.cpp
File metadata and controls
33 lines (29 loc) · 1011 Bytes
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
#include <sorted.hpp>
#include <iostream>
#include <vector>
#include <unordered_set>
#include <string>
int main() {
std::vector<int> vec = {19, 45, 32, 10, 0, 90, 15, 1, 7, 5, 6, 69};
std::cout << "sorted(vector): ";
for (auto&& i : iter::sorted(vec)) {
std::cout << i << ' ';
}
std::cout << '\n';
std::cout << "sorted by last character: ";
std::vector<std::string> svec = {"hello", "everyone", "thanks", "for",
"having", "me", "here", "today"};
for (auto&& s : iter::sorted(svec,
[] (const std::string& s1, const std::string& s2) {
return *s1.rbegin() < *s2.rbegin();})) {
std::cout << s << ' ';
}
std::cout << '\n';
// works even if the container isn't sortable
std::cout << "unordered_set<int> sorted: ";
std::unordered_set<int> uset = {10, 1, 20, 4, 50, 3};
for (auto&& i : iter::sorted(uset)) {
std::cout << i << ' ';
}
std::cout << '\n';
}