-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslide.cpp
71 lines (58 loc) · 1.81 KB
/
slide.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
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>
#if __cpp_lib_span
# include <span>
using std::span;
#else
# include <tcb/span.hpp>
using tcb::span;
#endif
// The example uses std::span to implement some algorithms on contiguous ranges.
template <class T, std::size_t N>
[[nodiscard]] constexpr auto slide(span<T, N> s, std::size_t offset, std::size_t width)
{
return s.subspan(offset, offset + width <= s.size() ? width : 0U);
}
template <class T> void print(const T& seq)
{
for (const auto& elem : seq) {
std::cout << elem << ' ';
}
std::cout << '\n';
}
void test()
{
constexpr int a[]{0, 1, 2, 3, 4, 5, 6, 7, 8};
// unused constexpr int b[] { 8, 7, 6 };
for (std::size_t offset{};; ++offset) {
constexpr std::size_t width{6};
auto s = slide(span{a}, offset, width);
if (s.empty()) {
break;
}
print(s);
}
}
int main()
{
// construction uses aggregate initialization
std::array<int, 3> a1{{1, 2, 3}}; // double-braces required in C++11 prior to
// the CWG 1270 revision (not needed in C++11
// after the revision and in C++14 and beyond)
std::array<int, 3> a2 = {1, 2, 3}; // double braces never required after =
std::array<std::string, 2> a3 = {std::string("a"), "b"};
// container operations are supported
std::sort(a1.begin(), a1.end());
std::reverse_copy(a2.begin(), a2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
// ranged for loop is supported
for (const auto& s : a3) {
std::cout << s << ' ';
}
// deduction guide for array creation (since C++17)
std::array a4{3.0, 1.0, 4.0}; // -> std::array<double, 3>
print(a4);
}