Structured Binding In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Structured Binding In C++

Structured Binding In C++

BLUF: Mastering Structured Binding In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Structured Binding In C++

C++ is renowned for its efficiency. Learn how Structured Binding In C++ enables low-level control and high-performance computing in the tutorial below.

The capability to declare multiple variables and associate them with the components of a tuple, pair, or any other class or array containing public data members is referred to as structured bindings in C++. This enhancement was introduced in C++17. While dealing with intricate data structures, this feature enhances the clarity and brevity of the code. Structured bindings make use of the std::get and std::tie functions in C++.

Before the introduction of structured bindings, it was essential to utilize std::get or std::tie for accessing particular members within a tuple or pair. This approach was crucial for retrieving values from these data structures, even though it might have made the code less clear. With structured bindings, developers now have a more elegant syntax for breaking down and extracting values from tuples and pairs.

Syntax:

The auto keyword and square brackets () are components of the C++ syntax for structured bindings.

Example

auto [variable1, variable2, ...] = expression;

Parameters:

  • variable1, variable2, etc.: These variables will be made and bound to the structure’s elements on the right side: variable1, variable2, etc.
  • expression: The structure, such as a tuple, pair, array, or user-defined type with public data members, is represented by this expression.

Arrays, tuples, pairs, and custom data structures with publicly accessible data members can be conveniently deconstructed using structured bindings.

Examples:

Let's consider multiple instances to demonstrate the concept of structure binding in C++.

1. Example with Tuple

Example

#include <iostream>
#include <tuple>
int main()
{
    std::tuple<int, double, std::string> myTuple{42, 3.14, "Hello"};
    // Using structured bindings
    auto [value1, value2, value3] = myTuple;
    // Displaying the values
    std::cout << "Value 1: " << value1 << std::endl;
    std::cout << "Value 2: " << value2 << std::endl;
    std::cout << "Value 3: " << value3 << std::endl;
    return 0;
}

Output:

Output

Value 1: 42
Value 2: 3.14
Value 3: Hello

Explanation:

  • The code contains the std::tuple template class and the headers required for input/output operations.
  • Defines a tuple called myTuple with three components of the types double, int, and std::string; the elements are initialised with 42, 3.14, and "Hello" in that order.
  • Applies the structured bindings of C++17 to the tuple to extract the values into the variables value1, value2, and value3.
  • Outputs the variable values to the console with the labels "Value 1," "Value 2," and "Value 3."
  • A return code of 0 indicates that the programme has terminated successfully.
  • 2. Example with Pair

Example

#include <iostream>
#include <utility>
int main() 
{
    std::pair<int, double> myPair{42, 3.14};
    // Using structured bindings
    auto [value1, value2] = myPair;
    // Displaying the values
    std::cout << "Value 1: " << value1 << std::endl;
    std::cout << "Value 2: " << value2 << std::endl;
    return 0;
}

Output:

Output

Value 1: 42
Value 2: 3.14

Explanation:

  • Along with the std::pair template class, the code contains the headers required for input/output functions.
  • Declares a pair called myPair with two components of the double and int types, initialized to 42 and 3.14, correspondingly.
  • The values from the pair are broken down into variables value1 and value2 using the structured bindings of C++17.
  • Publish the variable values to the console with "Value 1" and "Value 2."
  • It returns 0 to indicate that the programme has successfully terminated.
  • 3. Example with Array

Example

#include <iostream>
int main() 
{
    int myArray[] = {1, 2, 3, 4};
    // Using structured bindings
    auto [a, b, c, d] = myArray;
    // Displaying array values
    std::cout << "a: " << a << std::endl;
    std::cout << "b: " << b << std::endl;
    std::cout << "c: " << c << std::endl;
    std::cout << "d: " << d << std::endl;
    return 0;
}

Output:

Output

a: 1
b: 2
c: 3
d: 4

Explanation:

  • The header required for the input/output operations is included in the code.
  • Declares a four-integer array called myArray with the initial values set to 1, 2, 3, and 4.
  • Utilises the structured bindings in C++17 to divide the values in the array into the variables a, b, c, and d.
  • Variable values are printed to the console with the labels "a", "b", "c", and "d".
  • A return code of 0 indicates that the programme has terminated successfully.
  • 4. Example with User-Defined Type

Example

#include <iostream>
struct MyStruct 
{
    int x;
    double y;
};
int main()
{
    MyStruct myObject{42, 3.14};
    // Using structured bindings
    auto [xValue, yValue] = myObject;
    // Displaying the values
    std::cout << "x_Value: " << xValue << std::endl;
    std::cout << "y_Value: " << yValue << std::endl;
    return 0;
}

Output:

Output

x_Value: 42
y_Value: 3.14

Explanation:

  • The required header for input/output operations is included in the code.
  • Defines a structure named MyStruct and two members: a double (y) and an integer (x).
  • Sets the initial x and y values for an object of type MyStruct called myObject to 42 and 3.14, respectively.
  • The structured bindings in C++17 divide the values from the struct into the variables xValue and yValue.
  • Outputs the variable values to the console with the labels "xValue" and "yValue".
  • Shows a return code 0 to indicate that the programme has terminated successfully.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience