- 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
推薦閱讀
- 深度實(shí)踐OpenStack:基于Python的OpenStack組件開發(fā)
- Flask Blueprints
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)實(shí)踐教程
- 騰訊iOS測試實(shí)踐
- Java入門很輕松(微課超值版)
- 愛上micro:bit
- Java程序設(shè)計(jì)案例教程
- 機(jī)器學(xué)習(xí)微積分一本通(Python版)
- Natural Language Processing with Python Quick Start Guide
- Apache Solr PHP Integration
- 嵌入式C編程實(shí)戰(zhàn)
- Ubuntu Server Cookbook
- Implementing NetScaler VPX?(Second Edition)
- Daniel Arbuckle's Mastering Python
- The Java Workshop