Structured binding is a new feature since c++17
cppreference.com
auto [ identifier-list ] = expression
Bind array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| int main(int argc, char const *argv[])
{
int test[3] = {1, 2, 3};
auto [a, b, c] = test; // an new array e copy from test and a = e[0]; b = e[1]; c = e[2];
auto &[x, y, z] = test; // x = test[0]; x = test[1]; x = test[2]
cout << ++a << " " << ++b << " " << ++c << " " << endl;
for (int &i : test)
cout << i << " ";
cout << endl;
cout << ++x << " " << ++y << " " << ++z << " " << endl;
for (int &i : test)
cout << i << " ";
cout << endl;
return 0;
}
|
tuple
1
2
3
4
5
6
7
8
9
10
11
12
| int main(int argc, char const *argv[])
{
tuple<int, int, int> test(1, 2, 3);
auto &[a, b, c] = test; // 1 2 3
pair<int, char> test2(1, 'c');
auto &[a2, b2] = test2; // 1 c
return 0;
}
|
struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| struct test
{
int a;
int b;
};
int main(int argc, char const *argv[])
{
test one;
one.a = 1;
one.b = 2;
auto &[first, second] = one; // 1 ,2
return 0;
}
|