- 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
推薦閱讀
- C++程序設計教程
- 大學計算機基礎(第二版)
- Offer來了:Java面試核心知識點精講(原理篇)
- PHP+MySQL網站開發技術項目式教程(第2版)
- SEO智慧
- Mastering Kali Linux for Web Penetration Testing
- Mastering Yii
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- 愛上micro:bit
- 詳解MATLAB圖形繪制技術
- 智能搜索和推薦系統:原理、算法與應用
- 從Power BI到Analysis Services:企業級數據分析實戰
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- Hadoop大數據分析技術
- 從零開始構建深度前饋神經網絡:Python+TensorFlow 2.x