C++的string的这个find成员函数是怎么回事啊?
- C/C++ code
string replace_string(string &strSrc, const string &strOld, const string &strNew){ string::size_type pos(0); while (pos = strSrc.find(strOld , pos) != string::npos) { strSrc.replace(pos, strOld.length(), strNew); pos += strNew.length(); cout << pos << '\t' << strSrc << endl; } return strSrc;}int main(void){ string strTest("2001,1,1,23,12,3,1,33,12,"); string strOut = replace_string(strTest, ",", "|"); cout << strOut << endl; system("pause"); return 0;}一直都无法正常输出,查了一下,原来在replace_string函数里死循环了
输出的一直都是1 2 01,1,1,23,12,3,1,33,12,
我不明白,find无法在2001,1,1,23,12,3,1,33,12,这样一个字符串里正常查找到逗号这个字符
[解决办法]
while ((pos = strSrc.find(strOld , pos)) != string::npos)