C++ 输出变量的类型 使用typeid().name()函数
#include<iostream>#include<typeinfo>#include<cstring>using namespace std;int main(){int i;bool a;char b;short c;int d;long e;float f;doubl...
·
#include<iostream>
#include<typeinfo>
#include<cstring>
using namespace std;
int main(){
int i;
bool a;
char b;
short c;
int d;
long e;
float f;
double g;
long long h;
cout<<typeid(i).name()<<endl;
cout<<typeid(a).name()<<endl;
cout<<typeid(b).name()<<endl;
cout<<typeid(c).name()<<endl;
cout<<typeid(d).name()<<endl;
cout<<typeid(e).name()<<endl;
cout<<typeid(f).name()<<endl;
cout<<typeid(g).name()<<endl;
cout<<typeid(h).name()<<endl;
return 0;
}
输出结果:
其中 , i 表示int,b=bool c=char s=short l=long f=float, d=double x=long long
注意添加头文件:#include<typeinfo>
在上头文件中定义了typeid()操作符可以输出变量的类型。

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)