c++如何查询变量的类型
使用typeid().name()查询:
需要添加头文件typeinfo
#include <iostream> #include <typeinfo> using namespace std; int main() { int a; char ch; char name[20]; string str; double b; long c; long long d; bool e; cout << "The show of int is " << typeid(a).name() << endl; cout << "The show of char is " << typeid(ch).name() << endl; cout << "The show of char array is " << typeid(name).name() << endl; cout << "The show of string is " << typeid(str).name() << endl; cout << "The show of double is " << typeid(b).name() << endl; cout << "The show of long is " << typeid(c).name() << endl; cout << "The show of long long is " << typeid(d).name() << endl; cout << "The show of bool is " << typeid(e).name() << endl; return 0; }运行结果:
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: C++变量类型
- 下一篇: C++语言的基本符号与词汇