这是一个简单的示例程序:
#include <iostream>
#include <string>
using namespace std;
string replaceSubstring(string, string, string);
int main()
{
string str1, str2, str3;
cout << "These are the strings: " << endl;
cout << "str1: \"the dog jumped over the fence\"" << endl;
cout << "str2: \"the\"" << endl;
cout << "str3: \"that\"" << endl << endl;
cout << "This program will search str1 for str2 and replace it with str3\n\n";
cout << "The new str1: " << replaceSubstring(str1, str2, str3);
cout << endl << endl;
}
string replaceSubstring(string s1, string s2, string s3)
{
int index = s1.find(s2, 0);
s1.replace(index, s2.length(), s3);
return s1;
}
它编译但是该函数不返回任何内容。如果我更改return s1
为return "asdf"
它将返回asdf
。如何使用此函数返回字符串?