C++ 中的型別推斷是什麼?


型別推斷或演繹是指程式語言中自動檢測表示式的型別。這是某些強型別的靜態型別語言的特性。在 C++ 中,自動關鍵字(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;
}

在上面的程式中,它會自動獲取型別 std:: vector<int>:: iterator。

更新時間: 2020 年 2 月 11 日

640 次瀏覽

開啟你的 職業

完成課程即可獲得認證

立即開始
廣告