-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpp-range.cpp
37 lines (33 loc) · 940 Bytes
/
cpp-range.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
// cpp-range.cpp : demonstration of range views and actions
// note: compile with -std=c++17 or later
#include <range/v3/action.hpp>
#include <range/v3/view.hpp>
#include <iostream>
#include <string_view>
#include <cctype>
#include <vector>
int main() {
using namespace ranges;
auto fruits = {
"Apple", "pear", "banana", "MELON", "Pomegranate", "Orange", "asparagus"
};
auto selected_fruits = fruits
| views::filter(
[](std::string_view elem){
return toupper(elem.front()) <= 'M';
})
| views::transform(
[](std::string_view elem){
std::string s{};
for (auto c : elem) {
s += toupper(c);
}
return s;
})
| to<std::vector>()
| actions::sort
;
for (const auto& f : selected_fruits) {
std::cout << f << '\n';
}
}