-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTuple.cpp
More file actions
192 lines (150 loc) · 5.36 KB
/
Tuple.cpp
File metadata and controls
192 lines (150 loc) · 5.36 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// =====================================================================================
// Tuple.cpp // std::tuple
// =====================================================================================
module modern_cpp:tuple;
namespace TupleSamples {
// =======================================================
// demonstrating simple operations on a std::tuple
static void test_01()
{
// declaring tuple
std::tuple <char, int, double> values;
// assigning values to tuple using std::make_tuple
values = std::make_tuple('A', 123, 123.456);
// just in one statement
std::tuple<char, int, double> moreValues{ 'Z', 987, 987.654 };
// accessing tuple values using std::get
{
auto value1{ std::get<0>(values) };
auto value2{ std::get<1>(values) };
auto value3{ std::get<2>(values) };
std::print("The values of tuple are : ");
std::println("{} - {} - {}", value1, value2, value3);
}
// use std::get to change single values of a tuple
std::get<0>(values) = 'M';
std::get<2>(values) = 135.79;
// printing tuple values again
{
auto value1{ std::get<0>(values) };
auto value2{ std::get<1>(values) };
auto value3{ std::get<2>(values) };
std::print("The modified values of tuple are : ");
std::println("{} - {} - {}", value1, value2, value3);
}
}
// =======================================================
// demonstrating use of std::vector with std::tuple elements
using Row = std::tuple<int, char, double, std::string>;
static std::string rowToString(const Row& row)
{
int n = std::get<0>(row);
char ch = std::get<1>(row);
double d = std::get<2>(row);
std::string s = std::get<3>(row);
return "Id: " +
std::to_string(n) + ", " + std::to_string(ch) + ", " +
std::to_string(d) + ", " + s;
}
static void test_02()
{
Row row1{ 10, 'A', 1.11, "Mueller" };
Row row2{ 11, 'B', 2.22, "Sepp" };
Row row3{ 12, 'C', 3.33, "Hans" };
std::vector<Row> mySheet;
mySheet.push_back(row1);
mySheet.push_back(row2);
mySheet.push_back(row3);
for (const Row& row : mySheet) {
std::println("{}", rowToString(row));
}
}
// =======================================================
// same example, but using C++ 17 structured binding
static void test_03()
{
Row row1{ 10, 'A', 1.11, "Mueller" };
Row row2{ 11, 'B', 2.22, "Sepp" };
Row row3{ 12, 'C', 3.33, "Hans" };
std::vector<Row> mySheet;
mySheet.push_back(row1);
mySheet.push_back(row2);
mySheet.push_back(row3);
// C++ 17: structured binding
const auto& [id, abbr, val, name] = mySheet[0];
std::println("Id: {}", id);
std::println("Abbr: {}", abbr);
std::println("Value: {}", val);
std::println("Name: {}", name);
for (const auto& [id, abbr, val, name] : mySheet)
{
std::println("Id: {}", id);
std::println("Abbr: {}", abbr);
std::println("Value: {}", val);
std::println("Name: {}", name);
}
}
static void test_04()
{
Row row1{ 10, 'A', 1.11, "Mueller" };
Row row2{ 11, 'B', 2.22, "Sepp" };
Row row3{ 12, 'C', 3.33, "Hans" };
std::vector<Row> mySheet;
mySheet.push_back(row1);
mySheet.push_back(row2);
mySheet.push_back(row3);
// C++ 14: std::tie
int id{};
char abbr{};
double val{};
std::string name{};
std::tie(id, abbr, val, name) = mySheet[0];
// or (note: std::ignore)
//
// std::tie(id, std::ignore, val, name) = mySheet[0];
std::println("Id: {}", id);
std::println("Abbr: {}", abbr);
std::println("Value: {}", val);
std::println("Name: {}", name);
for (const auto& row : mySheet)
{
std::tie(id, abbr, val, name) = row;
std::println("Id: {}", id);
std::println("Abbr: {}", abbr);
std::println("Value: {}", val);
std::println("Name: {}", name);
}
}
// =======================================================
// retrieving number of elements of a std::tuple
static void test_05()
{
// declaring tuple
std::tuple <char, int, double> tuple { 'A', 123, 123.456 };
// retrieve number of elements with std::tuple_size
std::println("std::tuple size: {}",
std::tuple_size<decltype(tuple)>::value
);
// or
using MyTuple = std::tuple<char, int, double>;
std::println("std::tuple size: {}",
std::tuple_size<MyTuple>::value
);
// or
std::println("std::tuple size: {}",
std::tuple_size<std::tuple<char, int, double>>::value
);
}
}
void main_tuple()
{
using namespace TupleSamples;
test_01();
test_02();
test_03();
test_04();
test_05();
}
// =====================================================================================
// End-of-File
// =====================================================================================