C++ strcasecmp。无法解决的错误消息 #包括 #包括 #包括 #包括 使用名称空间std; int main() { 字符串s=“测试”; if(STRCAECMP,简称“测试”)) cout
C++ strcasecmp。无法解决的错误消息 #包括 #包括 #包括 #包括 使用名称空间std; int main() { 字符串s=“测试”; if(STRCAECMP,简称“测试”)) cout,c++,c++11,C++,C++11,strcasecmp将const char*作为参数,而不是std::string作为参数。使用strcasecmp(s.c_str(),“TEST”)代替,或者让s首先成为c样式字符串。这意味着strcasecmp需要一个c样式字符串(0终止的char[/code>)不是C++ 字符串。你可以用 S.CyString()/获得一个。 < p>可以尝试 #include <strings.h> #include <iostream> #include <string
strcasecmp
将const char*
作为参数,而不是std::string
作为参数。使用strcasecmp(s.c_str(),“TEST”)
代替,或者让s
首先成为c样式字符串。这意味着strcasecmp
需要一个c样式字符串(0
终止的char[/code>)不是C++ <代码>字符串。你可以用<代码> S.CyString()/<代码>获得一个。 < p>可以尝试
#include <strings.h>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string s = "test";
if(strcasecmp(s, "TEST"))
cout << "equals"<< endl;
return 0;
}
strcasecmp()
函数需要指向以null结尾的char
字符串的指针,您不能将std::string
直接传递给它
正确的使用方法应该是:
strcasecmp(s.c_str(), "TEST")
此外,程序中的代码看起来不正确,因为strcasecmp()
在字符串相等的情况下返回零。因此,应使用以下方法:
strcasecmp(s.c_str(), "TEST")
if(strcasecmp,“测试”)==0
请尝试使用strcasecmp(s.c_str(),“TEST”)
查看无法将字符串强制转换为字符*
if(strcasecmp(s, "TEST") == 0)
cout << "equals"<< endl;