1、变量的数据类型获取,需要包含头文件 typeinfo
2、使用typeid(),包含需要获取数据类型的变量名
3、使用成员函数name(),获取数据类型的名称
4、使用成员函数hash_code(),生成代表此类型的唯一哈希值(C++11)
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
int main()
{
int a = 1;
auto a_type = typeid(a).name(); // 获取类型名称
cout << "a_tpye = " << a_type << endl;
float b = 2;
float c = 3;
auto b_hash_code = typeid(b).hash_code(); // C++ 11中的新特性,返回该类型唯一的哈希值
auto c_hash_code = typeid(c).hash_code();
if (b_hash_code == c_hash_code)
{
cout << "b type == c type. hash code = " << b_hash_code << endl;
}
return EXIT_SUCCESS;
}
运行结果:
a_tpye = int
b type == c type. hash code = 3356228888
谢谢阅读