How can I insert another map into the map?
In the code I try to copy the map from another.
multimap<string, map<size_t, size_t>> sorted;
for (auto itr=m_Items.begin(); itr !=m_Items.end(); ++itr)
sorted.emplace(itr->first, make_pair(itr->second.m_Date.m_Time, itr->second.m_Cnt));
>Solution :
Assuming that you have m_Items as
struct Date {
std::size_t m_Time;
};
struct MyStruct
{
Date m_Date;
std::size_t m_Cnt;
};
std::multimap<std::string, MyStruct> m_Items;
then you need to
for (auto itr = m_Items.begin(); itr != m_Items.end(); ++itr)
sorted.emplace(itr->first, std::map<size_t, size_t>{ {itr->second.m_Date.m_Time, itr->second.m_Cnt} });
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Your map’s value is again a map (i.e. std::map<size_t, size_t>) not a std::par. Therefore, you need to insert as above.
In c++17 with the help of structured binding declaration, and with a range based for loop(since c++11), you could write much intuitive:
for (const auto& [key, value] : m_Items)
sorted.emplace(key, std::map<size_t, size_t>{ {value.m_Date.m_Time, value.m_Cnt} });
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here is a (short demo.)
However, if you only need an unsorted key-value as pair as entry in sorted, I would suggest a
std::multimap<std::string, std::pair<size_t, size_t>> sorted;
^^^^^^^^^^^^^^^^^^^^^^^^^^
instead there.