入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

c++如何查询变量的类型

创建时间:2014-03-19 投稿人: 浏览次数:4707

使用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;
}
运行结果:



声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像