- 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
推薦閱讀
- Java多線程編程實(shí)戰(zhàn)指南:設(shè)計(jì)模式篇(第2版)
- 自然語言處理實(shí)戰(zhàn):預(yù)訓(xùn)練模型應(yīng)用及其產(chǎn)品化
- 新一代通用視頻編碼H.266/VVC:原理、標(biāo)準(zhǔn)與實(shí)現(xiàn)
- 實(shí)戰(zhàn)低代碼
- Python數(shù)據(jù)可視化之Matplotlib與Pyecharts實(shí)戰(zhàn)
- Learning FuelPHP for Effective PHP Development
- C++寶典
- Scratch趣味編程:陪孩子像搭積木一樣學(xué)編程
- Kotlin Programming By Example
- Android Studio Cookbook
- Julia數(shù)據(jù)科學(xué)應(yīng)用
- 深度實(shí)踐KVM:核心技術(shù)、管理運(yùn)維、性能優(yōu)化與項(xiàng)目實(shí)施
- Go語言入門經(jīng)典
- Elasticsearch搜索引擎構(gòu)建入門與實(shí)戰(zhàn)
- Dart:Scalable Application Development