我们常常会使在字符串中替换所有某些字符串的操作:

int replace_all(string& str,const string& pattern, const string& newpat)

{

    int count = 0;

    const size_t nsize = newpat.size();

    const size_t psize = pattern.size();

    for(size_t pos = str.find(pattern, 0); pos != string::npos; pos = str.find(pattern,pos + nsize))

    {

        str.replace(pos, psize, newpat);

        count++;

    }

    return count;

}