How is the “replace” function used in C++?
In C++, the replace function is used to replace a character or substring at a specified position in a string. The syntax is as follows:
string replace (size_t pos, size_t len, const string& str);
In this case, pos represents the starting index for replacement, len represents the number of characters to be replaced, and str represents the string used for replacement. For example:
string str = "Hello, world!";
str.replace(7, 5, "C++"); // 将"world"替换为"C++"
cout << str; // 输出:Hello, C++!
Please note that the replace function will modify the original string.