简体   繁体   中英

How to merge std::unordered_map inside std::tuple?

I want to create a class which will have an std::tuple with std::unordered_map inside. I want to create a method which will merge maps inside tuple.

#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);
}

But I can't compile this code. I tried to create a simple example with un std::tuple of int 's and make a sum of them and it worked. But I stuck on this more complex example. Thank you for any suggestions.

The bigger problem I see is that std::unorderd_map<something...>::merge() return void .

So surely is wrong

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

I suggest to modify merge_map() , using template folding, as follows

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;
}

But also remember to include <string> and that the {.data = f} initialization syntax isn't C++17 (will be available starting from C++20, if I remember correctly).

Try this:

#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";
    }
}

Note, that merge_map doesn't return map, it modifies first one. So i've updated arguments and return values to reflect it.

This is how I solved it. I also made it c++14 friendly :)

#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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM