注册 登录
编程论坛 C++教室

string 替换字符的问题、、求大神看看

哒哒哒啦啦啦 发布于 2016-06-05 18:51, 3495 次点击
int main()
{
    string s = "a tho b thru c tho";
    string oldval = "tho";
    string newval = "through";

    auto curr = s.begin();

    while (curr <= (s.end() - oldval.size()))
    {
        string s1(curr, curr + oldval.size());
        if (s1 == oldval)
        {
            curr = s.replace(curr,oldval.size(),newval);******************这步一直报错,说参数有问题
            curr += newval.size();
            
        }
        else curr++;
    }cout << s << endl;
//    hanshu(s, oldval, newval);


    return 0;
}
3 回复
#2
yangfrancis2016-06-05 22:03
没用过。关注关注
#3
rjsp2016-06-06 08:30
下次代码要贴全,以免去别人重复打字的辛苦

程序代码:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s = "a tho b thru c tho";
    const string oldval = "tho";
    const string newval = "through";

    for( size_t pos=0; pos=s.find(oldval,pos),pos!=string::npos; pos+=newval.size() )
        s.replace( pos, oldval.size(), newval );

    cout << s << endl;

    return 0;
}

#4
哒哒哒啦啦啦2016-06-13 05:03
回复 3楼 rjsp
好的,谢谢~~
1