-
Notifications
You must be signed in to change notification settings - Fork 160
/
tuple.cpp
157 lines (121 loc) · 3.99 KB
/
tuple.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
# tuple
Hold a ordered collection of elements.
Each element can be of a different type.
The length is always fixed.
*/
#include "common.hpp"
int main() {
#if __cplusplus >= 201103L
// Create
{
// Constructor
{
std::tuple<int,char,std::string> t0(0, 'a', "a");
}
/*
# make_tuple
Analogous to make_pair, but with variable number of arguments. Pretty neat.
*/
{
std::tuple<int,char,std::string> t;
//without make_tuple
t = std::make_tuple(0, 'a', "a");
t = std::tuple<int,char,std::string>(0, 'a', "a");
//with make_tuple
}
// Tuple from pair.
{
std::tuple<int,char> t2(std::pair<int,char>(0, 'a'));
}
// Uniform initialization.
{
std::tuple<int,char,std::string> t{0, 'a', "a"};
}
// Fails because the tuple constructor are is `explicit`!
// TODO Rationale? http://stackoverflow.com/questions/14961809/returning-a-tuple-from-a-function-using-uniform-initialization-syntax
{
//std::tuple<int, int> t = {0, 1};
//std::tuple<int, int> t[]{{0, 1}};
}
}
/*
# get
Get single element from tuple.
Returns references, so it is possible to modify the tuples with them.
Copies are made from input elements
*/
{
std::tuple<int,std::string> t(0, "abc");
assert(std::get<0>(t) == 0);
assert(std::get<1>(t) == "abc");
// Can also be used to set tuple values.
std::get<0>(t) = 1;
assert(std::get<0>(t) == 1);
// Or you can modify contained objects.
std::get<1>(t)[0] = '0';
assert(std::get<1>(t) == "0bc");
}
/*
# tie
Unpack a tuple.
Unpack by reference seems not to be possible: <http://stackoverflow.com/questions/16571883/unpacking-a-std-tuple-into-pointers>
# ignore
Magic that exists only to ignore one of tie outputs.
*/
{
int i;
std::string s;
std::tuple<int,float,std::string> t(1, 1.5, "abc");
std::tie(i, std::ignore, s) = t;
assert(i == 1);
assert(s == "abc");
// Clearly copies are made.
i = 2;
assert(std::get<0>(t) == 1);
// Tie and declare variables at the same time. Nope?
// Tie saves assignments, but repeats types which we can get rid of with auto.
// If only we could save both types and assignments...
// http://stackoverflow.com/questions/29312154/how-to-create-a-new-variable-and-use-stdtie-at-the-same-time
// http://codereview.stackexchange.com/questions/61499/a-tuple-unpacking-macro-that-approximates-python-elegance
// Structured bindings
// https://stackoverflow.com/questions/29312154/how-to-create-a-new-variable-and-use-stdtie-at-the-same-time
{
#if __cplusplus >= 201703L
auto tuple = std::make_tuple(1, 'a', 2.3);
auto [i, c, d] = tuple;
assert(i == 1);
assert(c == 'a');
assert(d == 2.3);
#endif
}
}
/*
Relational operators operations are implemented
http://www.cplusplus.com/reference/tuple/tuple/operators/>
`<` family is lexicographical.
*/
{
std::tuple<int,char> t0(0, 'a');
std::tuple<int,char> t1(0, 'a');
std::tuple<int,char> t2(1, 'b');
std::tuple<int,char> t3(-1, 'b');
std::tuple<int,char> t4(0, 'b');
assert(t0 == t1);
assert(t0 != t2);
assert(t0 < t2);
assert(t0 > t3); //-1 counts
assert(t0 < t4); //0 ties, 'a' < 'b'
}
// Swap contents of two tuples of same type
{
std::tuple<int,char> t0(0, 'a');
std::tuple<int,char> t1(1, 'b');
std::tuple<int,char> old_t0 = t0;
std::tuple<int,char> old_t1 = t1;
t0.swap(t1);
assert(t0 == old_t1);
assert(t1 == old_t0);
}
#endif
}