如何加入两个或多个助推融合贴图
How do I join two or more boost fusion maps?
我想从两种boost::fusion::map
类型创建一个关联序列。其中一个映射中包含的类型可能存在于另一个映射,如果是这种情况,我只想在结果序列中得到一个具有该键的类型。也就是说,我希望键在加入后是唯一的。
传统的联接操作似乎允许重复键,所以它似乎不是一个解决方案。有人知道我是怎么做到的吗?
// Here is what I've got:
using namespace boost::fusion;
map<
pair<int, int>,
pair<double, int>> Map1;
map<
pair<bool, int>,
pair<double, int>> Map2;
// I want to join Map1 with Map2 such that I have
static_assert(std::is_same<Map3, map<
pair<int, int>,
pair<double, int>,
pair<bool, int>>>::value, "");
您可能需要手动消除欺骗:在完整的c++14装备中在Coliru上直播
auto r =
as_map(
fold(
fold(m1, m2, [](auto accum, auto elem) { return erase_key<typename decltype(elem)::first_type>(accum); }),
m1,
[](auto accum, auto elem) { return insert(accum, boost::fusion::end(accum), elem); }
));
这太古怪了。如果你用函子代替lambdas,你会得到类似的结果:
auto r =
as_map(
fold(
fold(m1, m2, erase_corresponding()),
m1,
insert_helper()
));
一个简单的实现LiveOnColiru仍然依赖于初步的c++1y支持:
struct erase_corresponding {
template<typename T, typename U>
auto operator()(T map, U elem) const {
return boost::fusion::erase_key<typename U::first_type>(map);
}
};
struct insert_helper {
template<typename T, typename U>
auto operator()(T map, U elem) const {
return boost::fusion::insert(map, boost::fusion::end(map), elem);
}
};
然而,要使其完全符合c++03,您需要使用RESULT_OF(我留给读者练习)
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 比较两个大小不等的映射c++
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- photoshop如何将两个图像融合在一起
- 在std::list中合并(将两个项目融合在一起,用融合替换)项目的算法(即破坏性聚类)
- 如何加入两个或多个助推融合贴图