可以利用库函数typeid,来变量是否为整型变量;
运行时获知变量类型名称,可以使用 typeid(变量).name,需要注意不是所有编译器都输出"int"、"float"等之类的名称,对于这类的编译器可以这样使用:float f = 1.1f; if( typeid(f) == typeid(0.0f) ) ……
#include <typeinfo.h>
int a;
if (typeid(a) == typeid(int))
#include <iostream>
using namespace std;
int main(){
auto a = 10;
if(typeid(a) == typeid(int)){
cout << "a的数据类型是:整型" << endl;
}
auto str1 = "Hello World";
auto str2 = "Hello World"s;
if(typeid(str1) == typeid(const char*)){
cout << "str1的类型是:const char*" << endl;
}
if(typeid(str2) == typeid(string)){
cout << "str2的类型是:string" << endl;
}
}