首先引入頭文件: <typeinfo>
獲取變量類型的語句是:typeid(variable).name(),其中 “variable”是你定義的變量名稱。
#include <iostream> #include <typeinfo> using namespace std; int main(){ int v1 = 1; char v2 = 'a'; double v3 = 1; float v4 = 1.1; bool v5 = false; cout << typeid(v1).name() << endl; cout << typeid(v2).name() << endl; cout << typeid(v3).name() << endl; cout << typeid(v4).name() << endl; cout << typeid(v5).name() << endl; return 0; }


第一個圖是gcc編譯器的運行結果,輸出的是變量類型的首字母。
第二個圖是VC++編譯器的運行結果,輸出的是變量類型的全拼。
如果對你有幫助的話,可以點一下右下角的 “推薦” 喲 ~~~
