#include <string>
#include <iostream>

//replace the substring l to r for source string s, and put result in d
void replace(const std::string& s,const std::string& l,const std::string& r,std::string& d)
{
    size_t i=0, n=s.length(), nl=l.length();
    while( 1 )
    {
        size_t pos = s.find_first_of(l,i);
        if(std::string::npos==pos)
        {
            d += s.substr(i);
            break;
        }
        else
        {
            if(pos-i>0)
                d += s.substr(i,pos-i);
            d += r;
            i = pos + nl;
        }
    }
}

void main()
{
    std::string d;
    replace("ddwv", "def", "_", d);
    std::cout<<d;
}

相关文章:

  • 2019-06-12
  • 2021-07-20
  • 2021-07-04
  • 2021-11-15
  • 2022-02-22
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案