-
Notifications
You must be signed in to change notification settings - Fork 79
/
1.9视图.cpp
38 lines (30 loc) · 1.03 KB
/
1.9视图.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
#include<iostream>
#include<ranges>
#include<vector>
namespace stdv = std::views;
namespace stdr = std::ranges;
void print(stdr::range auto v) {
for (const auto& i : v)std::cout << i << ' ';
endl(std::cout);
}
int main() {
std::vector nums{ 1,2,3,4,5,6,7,8,9,10 };
auto ret = nums | stdv::take(5) | stdv::reverse;
print(ret);
auto ret2 = nums | stdv::filter([](int i) {return i > 6; });
print(ret2);
auto ret3 = nums | stdv::transform([](int i) {return i * i; });
print(ret3);
print(nums);//视图是不会改变原来的数据的
std::vector<std::string>strs{ "🐴","🐭","🥵","🤣" };
auto ret4 = strs | stdv::reverse;
print(ret4);
auto ret5 = nums | stdv::filter([](int i) {return i % 2 != 0; }) | stdv::transform([](int i) {return i * 2; });
print(ret5);
auto nums_ = stdv::iota(1, 10);
print(nums_);
auto rnums = stdv::iota(1) | stdv::take(200);
print(rnums);
stdr::copy(strs | stdv::reverse | stdv::drop(2), std::ostream_iterator<std::string>(std::cout," "));
}
//范围库: https://zh.cppreference.com/w/cpp/ranges