-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspan.cpp
More file actions
83 lines (62 loc) · 1.59 KB
/
span.cpp
File metadata and controls
83 lines (62 loc) · 1.59 KB
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
#include <span>
#include <vector>
#include <string>
#include <iostream>
#include <ranges>
#include <cstdio>
template <typename T>
void print(const std::span<T> s) {
for (auto& item : s)
std::cout << item << " ";
std::cout << '\n';
}
void sub_first() {
const std::string text = "hakan gedek";
const std::span s {text};
const std::span<const char> first_3 = s.first<3>();
std::cout << first_3.data() << std::endl;
print(first_3);
std::cout << s.first(3).data() << std::endl;
}
void sub_last() {
const std::string text = "hakan gedek";
const std::span s{text};
std::cout << s.last<3>().data() << std::endl;
std::cout << s.last(3).data() << std::endl;
const std::span<const char, 3> last_3 = s.last<3>();
std::cout << last_3.data() << std::endl;
}
void reverse() {
std::string text = "hakan gedek";
std::span s {text};
for (std::size_t i = 0, j = s.size()-1; i < j; i++,j--)
std::swap(s[i], s[j]);
print(s);
}
void subs() {
const std::string text {"hakan gedek"};
const std::span s {text};
for (size_t i = 0; i < s.size(); i++) {
const auto ss = s.subspan(i, 3);
std::for_each(ss.begin(), ss.end(), std::putchar);
std::putchar('\n');
}
std::putchar('\n');
}
int main() {
std::vector v {0,1,2,3,4,5,6};
std::string s {"hakan"};
int A[5] = {0,1,2,3,4};
print(std::span{v});
print(std::span{s});
print(std::span{std::begin(A), std::end(A)});
// print(std::span{A});
sub_last();
sub_first();
reverse();
subs();
const std::string data = "Hakan Gedek";
const std::span sp {data};
std::cout << data << std::endl;
return 0;
}