我必须在String中搜索指定范围内的特定字符.我的代码如下:
str.erase(str.begin(), str.begin()+5);
str.erase(str.begin() + 9, str.end()); // The range to be searched has been specified ie 5th index to 9th index
if( str.find('a') != string::npos){
cout << "YES, it exists" << endl;
}else{
cout << "No, it doesnt" << endl;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码按预期工作,但我很想知道是否有一个标准的库函数可以完成这项工作.(可能在一行)
...但我很想知道是否有标准的库函数可以完成这项工作.(可能在一行)
您可以std::find()在不更改原始字符串变量的情况下执行此操作:
if(std::find(std::begin(str) + 5, std::end(str) - 9,'a') != std::end(str) - 9){
cout << "YES, it exists" << endl;
}else{
cout << "No, it doesnt" << endl;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99 次 |
| 最近记录: |