-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranges_demo.cpp
139 lines (111 loc) · 3.52 KB
/
ranges_demo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Demos MSVC STL implementation of The One Ranges Proposal: https://wg21.link/P0896R4
#include <algorithm>
#include <array>
#include <iostream>
#include <ranges>
#include <string_view>
#include <utility>
#include <vector>
using namespace std::literals;
template<typename T, typename R>
constexpr void print_range(const char* message, const T& list, const R& result)
{
std::cout << message << " [ ";
for (auto iter = list; iter != result.out; ++iter)
{
std::cout << *iter << ", ";
}
std::cout << " ]" << std::endl;
}
constexpr auto get_first = [](auto&& x) -> auto&& {
return static_cast<decltype(x)>(x).first;
};
constexpr auto get_second = [](auto&& x) -> auto&& {
return static_cast<decltype(x)>(x).second;
};
template<typename T>
void print_pairs(const char* message, const T& pairs)
{
std::cout << message << " [ ";
for (auto const& pair : pairs)
{
std::cout << "[" << pair.first << ", " << pair.second << "], ";
}
std::cout << " ]" << std::endl;
}
void ranges_copy_demo()
{
std::cout << "\nstd::ranges::copy() demo\n";
int const input[] = { 1, 2, 3, 5, 8, 13, 21, 34, 45, 79 };
int output[10] = { 0 };
auto range1 = std::ranges::copy(input, output);
print_range("copy output", output, range1);
// only copy numbers divisible by 3
auto range2 = std::ranges::copy_if(input, output, [](const int i) {
return i % 3 == 0;
});
print_range("copy_if %3 output", output, range2);
}
void ranges_find_demo()
{
std::cout << "\nstd::ranges::find() demo\n";
std::array<std::pair<int, int>, 3> pairs = { {{17, 10}, {23, 13}, {0, 23}} };
print_pairs("\nPairs", pairs);
auto result = std::ranges::find(pairs, 73, get_first);
if (result != std::end(pairs))
{
std::cout << "Found {73, *} : " << "{ " << result->first << ", " << result->second << " }" << std::endl;
}
else
{
std::cout << "{73, *} Not found!" << std::endl;
}
result = std::ranges::find(pairs, 23, get_second);
if (result != std::end(pairs))
{
std::cout << "Found {*, 23} : " << "{ " << result->first << ", " << result->second << " }" << std::endl;
}
else
{
std::cout << "{*, 23} Not found!" << std::endl;
}
}
void ranges_algorithms_demo()
{
ranges_copy_demo();
ranges_find_demo();
}
void ranges_views_adapters_demo()
{
// Examples based on https://en.cppreference.com/w/cpp/ranges
std::cout << "\nstd::ranges::views::counted() demo\n";
// A counted view presents a view of the elements of the counted range [i, n) for some iterator i and non-negative integer n.
const auto list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i : std::ranges::views::counted(list.begin() + 2, 5))
std::cout << i << ' ';
std::cout << std::endl;
std::cout << "\nstd::ranges::views::split() demo\n";
// split view takes a view and a delimiter, and splits the view into subranges on the delimiter
constexpr std::string_view demo_string { "This is a demo of C++20 Ranges"};
std::ranges::for_each(demo_string | std::ranges::views::split(' '), [](auto const& view) {
for (std::cout << "["; const auto element : view)
std::cout << element;
std::cout << "] ";
});
std::cout << std::endl;
std::cout << "\nstd::ranges::views::join() demo\n";
const auto url_parts = { "https"sv, "://"sv, "www.mywebsite.com"sv, "/"sv, "index.html"sv };
for (char const part : url_parts | std::ranges::views::join)
{
std::cout << part;
}
std::cout << std::endl;
}
void ranges_demo()
{
std::cout << "\nstd::ranges demos:\n";
ranges_algorithms_demo();
ranges_views_adapters_demo();
}