[Solved] Replace N formulas to one (string interpolation)


It looks like you’re asking for interpolation. In which case sure!
First you’ll want to construct a map<string, string> of the keys and values, for example:

map<string, string> interpolate = { { "F"s, "a && b && c"s }, { "H"s, "p ^ 2 + w"s }, { "K"s, "H > 10 || e < 5"s }, { "J"s, "F && !K"s } };

Then just use for_each,
stding::find, and string::replace.

for(const auto& i : interpolate) for_each(begin(interpolate), end(interpolate), [&](auto& it){ for(auto pos = it.second.find(i.first); pos != string::npos; pos = it.second.find(i.first, pos)) it.second.replace(pos, i.first.size(), '(' + i.second + ')'); });

Live Example

After running this code, when outputting interpolate with for(const auto& i : interpolate) cout << i.first << " : " << i.second << endl You’ll get:

F : a && b && c
H : p ^ 2 + w
J : (a && b && c) && !((p ^ 2 + w) > 10 || e < 5)
K : (p ^ 2 + w) > 10 || e < 5

8

solved Replace N formulas to one (string interpolation)