【发布时间】:2016-08-01 15:30:49
【问题描述】:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(){
string a="asdasd";
if(!strchr(a,'a')) cout<<"yes";
return 0;
}
刚开始学习C++编程,不知道为什么会出现这行错误
if(!strchr(a,'a')) cout<<"yes";
但如果我尝试这样编写代码,它会运行得很好。
if(!strchr("asdasd",'a')) cout<<"yes";
我知道这是一个愚蠢的问题,但我真的不知道为什么..对不起..
【问题讨论】:
-
改用
if(!strchr(a.c_str(),'a')) -
使用
a.find()而不是strchr -
您查看过
strchr的文档吗? -
您应该始终向我们展示您遇到的确切错误。此外,您应该停止在 C++ 程序中使用 C 风格的字符串及其相关函数。
-
哦!谢谢你们,因为我看到其他人使用 strchr,所以我认为它也适用于 C++。