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

  • 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
主站蜘蛛池模板: 嘉黎县| 大洼县| 景谷| 邵武市| 陈巴尔虎旗| 东丰县| 营山县| 博爱县| 甘南县| 古蔺县| 大厂| 烟台市| 犍为县| 西和县| 都昌县| 沂南县| 西平县| 中宁县| 河曲县| 濉溪县| 银川市| 昌宁县| 永德县| 石狮市| 泸溪县| 曲水县| 蒲江县| 修武县| 泰和县| 交口县| 石屏县| 泰宁县| 阳曲县| 综艺| 天水市| 高陵县| 涪陵区| 光泽县| 广宁县| 彩票| 上犹县|