在C++中是否可以用另一个字符串替换字符串的一部分?
基本上,我想这样做:
QString string("hello $name");
string.replace("$name", "Somename");
Run Code Online (Sandbox Code Playgroud)
但我想使用标准C++库.
我怎么能用C++中的另一个子字符串替换字符串中的子字符串,我可以使用哪些函数?
eg: string test = "abc def abc def";
test.replace("abc", "hij").replace("def", "klm"); //replace occurrence of abc and def with other substring
Run Code Online (Sandbox Code Playgroud) 库中是否有标准算法可以完成以下 for 循环的工作?
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main( )
{
const char oldFillCharacter { '-' };
std::vector<char> vec( 10, oldFillCharacter ); // construct with 10 chars
// modify some of the elements
vec[1] = 'e';
vec[7] = 'x';
vec[9] = '{';
const char newFillCharacter { '#' };
for ( auto& elem : vec ) // change the fill character of the container
{
if ( elem == oldFillCharacter )
{
elem = newFillCharacter;
}
} …Run Code Online (Sandbox Code Playgroud)