我想知道“_”在字符串中的位置:
string str("BLA_BLABLA_BLA.txt");
类似于:
string::iterator it;
for ( it=str.begin() ; it < str.end(); it++ ){
if (*it == "_") //this goes wrong: pointer and integer comparison
{
pos(1) = it;
}
cout << *it << endl;
}
谢谢, 安德烈
最佳答案
请注意,"_"
是一个字符串文字,而'_'
是一个 字 rune 字。
如果您将迭代器解引用为字符串,您得到的是一个字符。当然,字符只能和character literals比,不能和string比 文字。
但是,正如其他人已经注意到的那样,您不应该自己实现这样的算法。它已经完成了一百万次,其中两次(std::string::find()
和 std::find()
)最终出现在 C++ 的标准库中。使用其中之一。
关于c++ - 如何检测 C++ 字符串中的 "_"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3725574/