利用map函数的用法写一段代码

输入一串字母ACBHGGHJ
利用map函数将HGG换成S
将结果输出
结果为ACBSHJ

#include <iostream>
#include <string>

using namespace std;

void map(string &str, const string &from, const string &to)
{
    string::size_type pos = 0;
    while ((pos = str.find(from, pos)) != string::npos)
    {
        str.replace(pos, from.length(), to);
        pos += to.length();
    }
}

int main()
{
    string str, from, to;
    cin >> str >> from >> to;
    map(str, from, to);
    cout << str << endl;
    return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
ACBHGGHJ
HGG
S
ACBSHJ

是Stringbuffer不行吗