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

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
主站蜘蛛池模板: 方正县| 大连市| 黑水县| 三明市| 陇川县| 天峨县| 大关县| 隆林| 枣强县| 潞西市| 咸宁市| 福州市| 卓资县| 汶川县| 四平市| 宁蒗| 和平县| 长岭县| 息烽县| 大悟县| 青河县| 涟水县| 黄龙县| 三河市| 荆州市| 轮台县| 新干县| 晴隆县| 阜南县| 绥阳县| 玉门市| 宜城市| 瑞丽市| 乌兰县| 织金县| 鲁甸县| 若尔盖县| 大姚县| 黔南| 温泉县| 聂荣县|