尽管参数正确,但简单的字符串替换仍会产生错误

rns*_*nso 0 c++ string replace

我正在尝试使用以下代码使用此处给出的详细信息替换字符串的一部分:

#include <iostream>
#include <string>
using namespace std ; 

int main(){
    string ori = "this is a test"; // to replace "is a" with "IS A"
    string part = "is a"; 
    int posn = ori.find(part); 
    int len = part.size(); 
    cout << posn << endl; 
    ori.replace(posn, len, ori, "IS A"); 
    cout << ori; 
}
Run Code Online (Sandbox Code Playgroud)

但是,它给出一个很长的错误,开始于:

rnreplacestr.cpp:11:36: error: no matching function for call to ‘std::__cxx11::basic_string<char>::replace(int&, int&, std::__cxx11::string&, const char [5])’
  ori.replace(posn, len, ori, "IS A");
                                    ^
In file included from /usr/include/c++/6/string:52:0,
                 from /usr/include/c++/6/bits/locale_classes.h:40,
                 from /usr/include/c++/6/bits/ios_base.h:41,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from rnreplacestr.cpp:1:
Run Code Online (Sandbox Code Playgroud)

问题在哪里,如何解决?谢谢你的帮助。

acr*_*075 8

错误消息非常正确-没有匹配功能。我认为您打算使用std :: string :: replace的三参数版本。

更改

ori.replace(posn, len, ori, "IS A"); 
Run Code Online (Sandbox Code Playgroud)

ori.replace(posn, len, "IS A"); 
Run Code Online (Sandbox Code Playgroud)