Mic*_*hoo 6 c++ string compare
对于我的情况,我必须检查2个字符串是否相同.我得到了万阿英,蒋达清,就是不管我投入,我得到一个真正的价值,无论是我插嘴说.
bool Dictionary::checkIfWordExists(string input){
for(int counter=0;counter<234;counter++){
if(input.compare(getWordFromDictionary(counter))){
return true;
}
}
return false;}
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我使用这样的do循环来输入要测试的东西,与我加载的dictionary.txt文件相比较.
do{
cout<<"enter something you sexy beast"<<endl;
string test;
cin>>test;
if(loadedDictionary.checkIfWordExists(test)){
cout<<"yes"<<endl;
}else{
cout<<"no"<<endl;
}
}while(true);
Run Code Online (Sandbox Code Playgroud)
Ove*_*erv 20
那是因为0当字符串相等时,compare实际返回.如果字符串不相等,它将返回一个更高或更低的值,if将评估为true,如您所见.
在Java和C#等语言中常见的方法是使用equals比较非基元的方法,但在C++中最好只使用==.
| 归档时间: |
|
| 查看次数: |
22502 次 |
| 最近记录: |