-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStructuredBinding.cpp
More file actions
160 lines (118 loc) · 3.67 KB
/
StructuredBinding.cpp
File metadata and controls
160 lines (118 loc) · 3.67 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
// =====================================================================================
// StructuredBinding.cpp // Structured Binding
// =====================================================================================
module modern_cpp:structured_binding;
namespace StructuredBinding {
static std::pair<int, int> divide_remainder(int dividend, int divisor)
{
int quotient{ dividend / divisor };
int remainder{ dividend % divisor };
std::pair<int, int> result{ quotient, remainder };
return result;
}
static void test_01()
{
auto result{ divide_remainder(16, 3) };
std::println("16 / 3 is {} with a remainder of {}",
result.first, result.second
);
}
static void test_02()
{
auto [quotient, remainder] { divide_remainder(20, 3) };
std::println("16 / 3 is {} with a remainder of {}", quotient, remainder);
}
static void test_03()
{
int arr[] { 123, 456, 789 };
auto [a, b, c] { arr };
std::println("{}, {}, {}", a, b, c);
}
static void test_04()
{
std::array<int, 3> arr { 123, 456, 789 };
auto [a, b, c] { arr };
std::println("{}, {}, {}", a, b, c);
}
static void test_05()
{
int arr[] { 123, 456, 789 };
auto& [a, b, c] { arr };
std::println("{}, {}, {}", a, b, c);
b = 999;
std::println("{}, {}, {}", arr[0], arr[1], arr[2]);
}
static void test_06()
{
std::array<int, 3> arr { 123, 456, 789 };
auto& [a, b, c] { arr };
std::println("{}, {}, {}", a, b, c);
b = 999;
std::println("{}, {}, {}", arr[0], arr[1], arr[2]);
}
struct Point
{
int m_x;
int m_y;
};
static void test_07() {
// without structured binding
Point p1 { 1, 2 };
std::println("X Coordinate : {}", p1.m_x);
std::println("Y Coordinate : {}", p1.m_y);
// with structured binding
Point p2 { 10, 20 };
auto [x, y] { p2 };
std::println("X Coordinate : {}", x);
std::println("Y Coordinate : {}", y);
// with structured binding and an anonymous object
auto [x1, y1] { Point{ 100, 200 } };
std::println("X Coordinate : {}", x1);
std::println("Y Coordinate : {}", y1);
}
static void test_08() {
Point p { 10, 20 };
auto& [x, y] { p };
std::println("X Coordinate : {}", x);
std::println("Y Coordinate : {}", y);
x = 100;
y = 200;
std::println("X Coordinate : {}", p.m_x);
std::println("Y Coordinate : {}", p.m_y);
}
struct Employee
{
unsigned int id;
std::string name;
std::string role;
unsigned long phone;
};
static void test_09() {
Employee worker { 9987, "Sepp", "Engineer", 987654321 };
Employee manager { 9999, "Hans", "Manager", 123456789 };
std::vector<Employee> employees { worker, manager };
for (const auto& [id, name, role, phone] : employees)
{
std::print("Id: {}, ", id);
std::print("Name: {}, ", name);
std::print("Role: {}, ", role);
std::println("Phone: {}", phone);
}
}
}
void main_structured_binding()
{
using namespace StructuredBinding;
test_01();
test_02();
test_03();
test_04();
test_05();
test_06();
test_07();
test_08();
test_09();
}
// =====================================================================================
// End-of-File
// =====================================================================================