-4
#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";

我知道这是一个愚蠢的问题,但我真的不知道为什么..对不起..

4

2 回答 2

3

库函数strchr用于 C 风格的字符串,而不是 C++string类型。

于 2016-08-01T15:32:38.290 回答
2

使用std::string时,最接近的等价物strchrfind

#include <iostream>
#include <string>

int main(){
    std::string a="asdasd";
    if(a.find('a') != std::string::npos) std::cout<<"yes";
} 
于 2016-08-01T16:27:06.593 回答