官术网_书友最值得收藏!

  • Mastering C++ Programming
  • Jeganathan Swaminathan
  • 172字
  • 2021-07-02 18:28:47

Structured binding

You can now initialize multiple variables with a return value with a really cool syntax, as shown in the following code sample:

#include <iostream>
#include <tuple>
using namespace std;

int main ( ) {

tuple<string,int> student("Sriram", 10);
auto [name, age] = student;

cout << "\nName of the student is " << name << endl;
cout << "Age of the student is " << age << endl;

return 0;
}

In the preceding program, the code highlighted in bold is the structured binding feature introduced in C++17. Interestingly, we have not declared the string name and int age variables. These are deduced automatically by the C++ compiler as string and int, which makes the C++ syntax just like any modern programming language, without losing its performance and system programming benefits. 

The preceding code can be compiled and the output can be viewed with the following commands:

g++-7 main.cpp -std=c++17
./a.out

The output of the preceding program is as follows:

Name of the student is Sriram
Age of the student is 10
主站蜘蛛池模板: 高台县| 宁化县| 呼玛县| 富宁县| 梓潼县| 潢川县| 额济纳旗| 阳东县| 如皋市| 永宁县| 安庆市| 额尔古纳市| 靖宇县| 陆川县| 军事| 太和县| 青州市| 姚安县| 南江县| 临泽县| 青冈县| 池州市| 乳山市| 紫金县| 长汀县| 雷州市| 偃师市| 蒙山县| 黔西县| 靖西县| 玉环县| 遂溪县| 宣城市| 连平县| 项城市| 鹤岗市| 容城县| 博湖县| 盐亭县| 唐河县| 鄂托克旗|