检查字符串值时"error: no match for operator=="

"error: no match for operator==" when checking the value of a string

本文关键字:match operator no for error 字符串 检查      更新时间:2023-10-16

如何检查字符串是否等于'♦'?

some_string设置为4并打印如下:

some_string = 4;
cout << some_string;

给我看这个:

但是我该怎么测试呢?我试过了,但不起作用:

if (some_string==4){
}

谢谢!

编辑:
如果您想测试字符代码为4的字符,那么您可以执行以下操作:

if(some_string == 'x04') {
    //Do something...
}

字符串以字符的形式进行输入。。

string some_string="4";

初始化字符串时总是使用"\0",也就是以NULL结尾的字符,因为它告诉字符串已经结束,否则程序将继续检查内存中的下一个连续位置,这将导致错误。

您可以使用关系运算符。。

if(some_string=="some_value")
{
//Do Something....
}