要比较两个类型信息的typeid()运算符,可以使用如下代码示例:
#include
#include
int main() {
int a = 5;
double b = 5.5;
// 使用typeid()比较两个类型信息
if (typeid(a) == typeid(int)) {
std::cout << "a is of type int" << std::endl;
}
if (typeid(b) == typeid(double)) {
std::cout << "b is of type double" << std::endl;
}
return 0;
}
在上面的代码示例中,我们定义了一个整数变量a和一个双精度浮点数变量b。然后,我们使用typeid()运算符将变量的类型信息与特定类型进行比较。如果类型匹配,我们将输出相应的消息。
在这个例子中,我们比较了a的类型是否为int,并输出了相应的消息。然后,我们比较了b的类型是否为double,同样输出了相应的消息。
注意,使用typeid()运算符比较类型时,需要包含头文件。