C++ 中的型別推斷
型別推斷或型別匯出是指自動檢測程式語言中表達式的型別。它是某些強靜態型別語言中的特徵。在 C++ 中,auto 關鍵字(在 C++ 11 中新增)用於自動型別匯出。例如,你想建立一個迭代器來迭代向量,可以簡單地使用 auto 來實現。
例子
#include<iostream> #include<vector> using namespace std; int main() { vector<int> arr(10); for(auto it = arr.begin(); it != arr.end(); it ++) { cin >> *it; } return 0; }
輸出
In the above program, it will automatically get the type std:: vector<int>:: iterator.
廣告