牛骨文教育服务平台(让学习变的简单)
博文笔记

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

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

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



声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。