将 N 个公式替换为一个(字符串插值)
Replace N formulas to one (string interpolation)
是否可以
在 c++ 中进行这样的字符串转换:
例:
从:
F = a && b && c;
H = p ^ 2 + w
K = H > 10 || e < 5;
J = F && !K;
自:
J = (a && b && c) && !( (p ^ 2 + w) > 10 || e < 5);
看起来您正在请求插值。在这种情况下,当然!首先,您需要构造键和值的map<string, string>
,例如:
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 } };
然后只需使用for_each
, stding::find
,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 + ')'); });
现场示例
运行此代码后,使用 for(const auto& i : interpolate) cout << i.first << " : " << i.second << endl
输出interpolate
时,您将获得:
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
相关文章:
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 将使用太多的纹理插值器 - 带旋转的着色器
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- 升压插值条件变量可以虚假唤醒吗?
- 如何获得插值极坐标?
- 如何线性插值到不恒定的目的地
- 向心Catmull-Rom样条曲线插值alpha参数
- 使用 OpenGL 插值数据缓冲区?
- 缩放和插值数组
- 骨骼动画:变换矩阵(collada)之间的插值
- 返回具有C++字符串返回值的函数的本地 C 字符串
- 从一组点C++平面插值
- C++ void 函数不更改字符串的值
- 从 C++14 到 C++98 的端口字符串插值
- 将矢量的数据扩展到指定大小(插值)
- 双线性插值实现的错误
- 如何避免线性插值"trap"?
- 使用 DirectX 11 插值背景颜色?
- OpenGL 缺陷 - 不需要的插值
- 将 N 个公式替换为一个(字符串插值)