対応表による文字列置換

best tool in Boost for (massive) string replacement?


Boost.Xpressiveを使用し、std::mapで文字列置換の対応表を作るという方法がすごく気に入りました。

#include <map>
#include <string>
#include <iostream>
#include <boost/xpressive/xpressive_static.hpp>
#include <boost/xpressive/regex_actions.hpp>

using namespace boost::xpressive;

int main()
{
    const std::map<std::string, std::string> rep = {
        {"\\alpha", "a"},
        {"\\beta", "b"},
        {"\\gamma", "g"},
        {"\\delta", "d"}
    };

    local<std::string const *> pstr;
    const sregex rx = (a1 = rep)[pstr = &a1];

    const std::string str("\\alpha \\beta \\gamma \\delta");
    std::cout << regex_replace(str, rx, *pstr) << std::endl;
}
a b g d