Structured Binding in C++
Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple variables from the struct. The primary purpose of structure binding is to make the code clean and easy to understand. The structured binding does not support reference type.
Syntax:
auto ref-operator(optional)[identifier-list] = expression;
// Or
auto ref-operator(optional)[identifier-list]{expression};
// Or
auto ref-operator(optional)[identifier-list](expression);
Parameter for structured binding:
- There is an auto parameter.
- The ref operator is used for either & or &&.
- The identifier list is used to list the comma variable.
- The expression does not have a comma operator and has the class type of array or non-union class.
Basic Type Deduction
It is either a type of std::tuple or a type of non-static data member. Non-static data member has accessible by others and is declared by some other class variable. In a Structured binding declaration, binding is possible in three different ways.
- Case-1: If we have an array type E, the name is bound with the array elements.
- Case-2: If we have a non-union class E and tuple_size is a complete type, then tuple_like protocol is implemented.
- Case-3: If we have a non-union class E and tuple_size is not a complete type, then the named bind with the public data member.
Example 1:
#include <bits/stdc++.h>
using namespace std;
struct Point {
int a;
int b;
};
int main()
{
Point p = {5, 8};
int a_coord = p.a;
int b_coord = p.b;
cout <<"The Coordinate of A is: "<< a_coord << endl;
cout <<"The Coordinate of B is: "<< b_coord << endl;
return 0;
}
Output:
The Coordinate of A is: 5
The Coordinate of B is: 8
Example 3:
This code runs on the C++17 version.
#include <bits/stdc++.h>
using namespace std;
struct Point
{
int a;
int b;
};
int main( )
{
Point k = { 5,8 };
auto[ a_coord, b_coord ] = k;
cout <<"The co-ordinate of A: "<< a_coord << endl;
cout <<"The co-ordinate of B: "<< b_coord << endl;
return 0;
}
Output:
The co-ordinate of A: 5
The co-ordinate of B: 8
Application of Structured Binding
With the help of structured binding, we can fetch the elements from an array. In this example, we are going to take E as an array. So with the help of structured binding, the name is associated with array E. Let's understand this with an example.
Example 4:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[3] = { 5 , 6 , 8 };
auto[a , b , c] = arr;
cout << a <<""<< b <<""<< c << endl;
return 0;
}
Output:
5 6 8
It should be noted that the number of elements in the list and elements in the array must be equal. If these two are not equal, the compiler throws the error.
Example 5:
#include <bits/stdc++.h>
#include <map>
using namespace std;
int main()
{
map<string, string> sites;
sites.insert({ "javaTpoint", "is a website for coding resource" });
sites.insert({ "tutorial and example", "is a website for coding resource" });
sites.insert({ "Wikipedia", "Resources + References" });
for (auto & [ key, value ] : sites)
{
cout << key.c_str() <<""<< value.c_str() << endl;
}
return 0;
}
Output:
Wikipedia Resources + References
javaTpoint is a website for coding resource
tutorial and example is a website for coding resource
Variables such as volatile and const are both used to declare the qualifier. This qualifier makes the type of declaration variable.