如何在std :: tuple中合并std :: unordered_map?

时间:2019-07-06 15:57:01

标签: c++ c++17 variadic-templates template-meta-programming unordered-map

我想创建一个包含std::tuplestd::unordered_map在内的类。 我想创建一个将元组中的地图合并的方法。

#include <tuple>
#include <unordered_map>

template <typename ... Ts, std::size_t ... Is>
std::tuple<Ts...> merge_map (std::tuple<Ts...>& t1, std::tuple<Ts...> & t2, std::index_sequence<Is...> const &) { 
    return { 
        (std::get<Is>(t1).merge( std::get<Is>(t2)))... 
    }; 
}

template <class ...Ts>
struct MyContainer
{
    std::tuple<Ts...> data;

    void merge(MyContainer<Ts...>& other){
        data = merge_map(data, other.data, std::make_index_sequence<sizeof...(Ts)>{});
    }

};

int main()
{
    using namespace std;
    unordered_map<string, int> f = {{"zero", 0}, {"one", 1}};
    MyContainer <unordered_map<string, int>> container1 = {.data = f};

    unordered_map<string, int> s = {{"two", 2}, {"three", 3}};
    MyContainer <unordered_map<string, int>> container2 = {.data = s};

    container1.merge(container2);
}

但是我不能编译这段代码。 我尝试用std::tuple的un int创建一个简单的示例,并对它们求和,然后开始工作。但是我坚持这个更复杂的例子。 谢谢您的任何建议。

3 个答案:

答案 0 :(得分:1)

我看到的更大的问题是std::unorderd_map<something...>::merge()返回void

所以肯定是错误的

return { 
    (std::get<Is>(t1).merge( std::get<Is>(t2)))... 
}; 

我建议如下使用模板折叠来修改merge_map()

template <typename ... Ts, std::size_t ... Is>
std::tuple<Ts...> merge_map (std::tuple<Ts...> & t1,
                             std::tuple<Ts...> & t2,
                             std::index_sequence<Is...> const &) { 
   (std::get<Is>(t1).merge(std::get<Is>(t2)), ...); 

   return t1;
}

但是也要记住包括<string>并且{.data = f}的初始化语法不是C ++ 17(如果我没记错的话,将从C ++ 20开始可用)。

答案 1 :(得分:0)

尝试一下:

#include <tuple>
#include <unordered_map>
#include <iostream>

template <typename T>
void merge_map_helper (T& t1, T& t2) { }
template <typename T, std::size_t I, std::size_t ... Is>
void merge_map_helper (T& t1, T& t2) { 
    std::get<I>(t1).merge( std::get<I>(t2));
    merge_map_helper<T, Is...>(t1, t2);
}

template <typename ... Ts, std::size_t ... Is>
void merge_map (std::tuple<Ts...>& t1, std::tuple<Ts...> & t2, std::index_sequence<Is...> const &) { 
    merge_map_helper<std::tuple<Ts...>, Is...>(t1, t2);
}

template <class ...Ts>
struct MyContainer
{
    std::tuple<Ts...> data;

    MyContainer(std::tuple<Ts...> data) : data(std::move(data)) { }

    void merge(MyContainer<Ts...>& other){
        merge_map(data, other.data, std::make_index_sequence<sizeof...(Ts)>{});
    }
};

int main()
{
    using namespace std;
    unordered_map<string, int> f1 = {{"zero1", 0}, {"one", 1}};
    unordered_map<string, int> f2 = {{"zero2", 0}, {"one", 1}};
    MyContainer <unordered_map<string, int>, unordered_map<string, int>> container1 = 
        { { f1, f2 } };

    unordered_map<string, int> s1 = {{"two1", 2}, {"three", 3}};
    unordered_map<string, int> s2 = {{"two2", 2}, {"three", 3}};
    MyContainer <unordered_map<string, int>, unordered_map<string, int>> container2 = 
        { { s1, s2} };

    container1.merge(container2);

    for(auto & k :std::get<0>(container1.data)) {
        std::cout << k.first << " " << k.second << "\n";
    }

    for(auto & k :std::get<1>(container1.data)) {
        std::cout << k.first << " " << k.second << "\n";
    }
}

请注意,merge_map不会返回map,它会修改第一个。因此,我更新了参数并返回值以反映它。

答案 2 :(得分:0)

这就是我解决的方法。我也使它变得c ++ 14友好:)

#include <tuple>
#include <unordered_map>



template <typename ... Ts, std::size_t ... Is>
auto merge_map (std::tuple<Ts...>& t1, std::tuple<Ts...> & t2, std::index_sequence<Is...> const &) { 
    return std::make_tuple( 
        std::get<Is>(t1)..., std::get<Is>(t2)...
    ); 
}

template <class ...Ts>
struct MyContainer
{
    std::tuple<Ts...> data;

};

template <class ...Ts>
auto merge(MyContainer<Ts...>& c1, MyContainer<Ts...>& c2){
   return merge_map(c1.data, c2.data, std::make_index_sequence<sizeof...(Ts)>{});
}

int main()
{
    using namespace std;
    unordered_map<string, int> f = {{"zero", 0}, {"one", 1}};
    MyContainer <unordered_map<string, int>> container1 = {.data = std::make_tuple(f)};

    unordered_map<string, int> s = {{"two", 2}, {"three", 3}};
    MyContainer <unordered_map<string, int>> container2 = {.data = std::make_tuple(s)};

    MyContainer<decltype(f), decltype(s)> cc;
    cc.data = merge(container1, container2);

}