C++中获取数据的类型

C++中获取数据的类型

  1. 执行该操作需要包含头文件;

  2. 变量 a 的数据类型: typeid(a).name();

  3. 常用的数据类型的结果:(不同的编译器,结果不同)

    在VS的MSVC编译器下结果:

    img

    在MinGw下的g++编译器下结果:

    img

  4. 执行代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    #include <typeinfo>
    int main()
    {
        
    
        int a = 10;
        float b = 1.0;
        double c = 11.0;
        char d = '1';
        bool e = true;
        std::string s = "123" ;
        std::cout << "type of a :" << typeid(a).name() << std::endl
                                        << "type of b :" << typeid(b).name() << std::endl
                                        << "type of c :" << typeid(c).name() << std::endl
                                        << "type of d :" << typeid(d).name() << std::endl
                                        << "type of e :" << typeid(e).name() << std::endl
                                        << "type of s :" << typeid(s).name() << std::endl;
    
        return 0;
    }
    
    
posted @ 2021-01-06 15:51  JemmyZhong  阅读(5615)  评论(0)    收藏  举报
levels of contents