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

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
主站蜘蛛池模板: 德惠市| 安康市| 隆林| 宜良县| 万荣县| 靖安县| 沙洋县| 罗平县| 堆龙德庆县| 南皮县| 青浦区| 崇仁县| 信丰县| 永康市| 汉沽区| 甘洛县| 攀枝花市| 会理县| 绥化市| 汾阳市| 二手房| 涡阳县| 邢台市| 那坡县| 平定县| 崇阳县| 河北省| 张家界市| 泰兴市| 当雄县| 瑞昌市| 墨竹工卡县| 百色市| 娱乐| 高安市| 资阳市| 满城县| 泰州市| 治县。| 镇巴县| 阳曲县|