gpt4 book ai didi

c++ - 如何加入两个或多个 boost fusion map ?

转载 作者:太空狗 更新时间:2023-10-29 21:19:50 25 4
gpt4 key购买 nike

我想创建一个 associative sequence来自两个 boost::fusion::map 类型。其中一个映射中包含的类型可能存在于另一个映射中,如果是这种情况,我只想在结果序列中以具有该键的单一类型结束。也就是说,我希望 keys 在加入后是唯一的。

常规join操作似乎允许重复键,因此它似乎不是解决方案。有谁知道我怎样才能做到这一点?

// 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 设备中 Live On 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); }
));

这很时髦。如果您将其替换为仿函数而不是 lambda,您最终会类似于:

auto r = 
as_map(
fold(
fold(m1, m2, erase_corresponding()),
m1,
insert_helper()
));

一个简单的实现 Live On Coliru 仍然依靠初步的 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++ - 如何加入两个或多个 boost fusion map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26099195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com