0
我在寻找替换全部算法,它在特定位置后替换所有出现的子字符串。到目前为止,我有replace all copy
的方法。除了this one之外,没有分配新字符串,最方便的方法是什么?是否存在方便的方法来提升呢?在特定位置后替换所有出现的搜索字符串
#include <iostream>
#include <string>
#include <boost/algorithm/string/replace.hpp>
int main() {
std::string str = "1234 abc1234 marker 1234 1234 123 1 12 134 12341234";
const std::string marker("marker");
size_t pos = str.find(marker);
if (pos == std::string::npos) {
return 0;
}
pos += marker.length();
std::string str_out(str, 0, pos);
boost::algorithm::replace_all_copy(std::back_inserter(str_out), str.substr(pos, std::string::npos), "12", "XXXX");
std::cout << str << std::endl;
std::cout << str_out << std::endl;
}