c++ - Printing multimap within multimap (nested maps) -
c++ - Printing multimap within multimap (nested maps) -
i trying print out nested multimap haven't found way (or give-and-take here) help me solve problem.
they way print multimap looks this:
template<typename a, typename b> void printmmap1(std::multimap<a, b>thing){ (std::multimap<a,b>::iterator = thing.begin(); != thing.end(); it++){ std::cout << it->first << " : " << it->second << std::endl; } }
but want utilize same motivation print a:
multimap<multimap<a,b>, multimap<c,d>> mapname;
this doesn't seem work:
template<typename aa, typename bb, typename cc> void printmmap(std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>thing){ (std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>::iterator = thing.begin(); != thing.end(); it++){ std::cout << it->first.first << " : " << it->first.second << std::endl << it->second.first << " : " << it->second.second << std::endl; }
}
any help/suggestions much appreciated!
thank help.
_edit:
using motivation hansmaad's example, came close solution (below):
//n.b: removed "auto"s educational purposes (mostly myself , other beginners) template<typename a, typename b> void print1(const std::multimap<a, b>& thing){ (std::multimap<a,b>::const_iterator = begin(thing); != thing.end(); it++){ std::cout << it->first << " : " << it->second << std::endl; } } template<typename aa, typename bb, typename cc> void print2(const std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>& thing){ std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>::const_reverse_iterator = thing.rbegin(); //why reverse iterator? because noticed loop duplicates output has final iteration equal desired output, utilize lastly iteration i.e. going backwards ("rbegin") std::multimap<aa, bb> keymap = it->first; std::multimap<aa, cc> valuemap = it->second; std::cout << "key\n"; print1(keymap); std::cout << "value\n"; print1(valuemap); }
this prints solution, 90% close want. e.g. prints:
key a_key1 : a_value1 a_key2 : a_value1 a_key2 : a_value2 a_key2 : a_value3 a_key3 : a_value1 a_key3 : a_value2 a_key3 : a_value3 a_key3 : a_value4 value b_key1 : b_value1 b_key1 : b_value2 b_key1 : b_value3 b_key1 : b_value4 b_key2 : b_value1 b_key3 : b_value1 b_key4 : b_value1 b_key4 : b_value2
whereas want print same output, albeit formatted so:
key value a_key1 : a_value1 b_key1 : b_value1 a_key2 : a_value1 b_key1 : b_value2 a_key2 : a_value2 b_key1 : b_value3 a_key2 : a_value3 b_key1 : b_value4 a_key3 : a_value1 b_key2 : b_value1 a_key3 : a_value2 b_key3 : b_value1 a_key3 : a_value3 b_key4 : b_value1 a_key3 : a_value4 b_key4 : b_value2
a slight difference. smell close.
i not sure expect output printmmap
function, problem code it->first.first
template<typename aa, typename bb, typename cc> void printmmap(std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>thing){ (std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>::iterator = thing.begin(); != thing.end(); it++){ // std::multimap<aa, bb> x = it->first; // std::multimap<aa, bb> y = it->second; // x, y don't have fellow member 'first', multimaps // broken: std::cout << it->first.first /*...*/ it->second.second << std::endl; } }
you example:
template<typename a, typename b> void print(const std::multimap<a, b>& thing){ (auto = begin(thing); != end(thing); ++it){ std::cout << it->first << " : " << it->second << std::endl; } } template<typename aa, typename bb, typename cc> void print(const std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>& thing){ (auto = begin(thing); != end(thing); ++it){ auto& keymap = it->first; auto& valuemap = it->second; std::cout << "key\n"; print(keymap); std::cout << "value\n"; print(valuemap); } }
which print for
multimap<multimap<int, int>, multimap<int, int>> mmap; multimap<int, int> key{ { 1, 1 }, { 1, 2 }, { 2, 4 } }; multimap<int, int> value{ { 10, 1 } }; mmap.emplace(move(key), move(value)); print(mmap); >key >1 : 1 >1 : 2 >2 : 4 >value >10 : 1
c++ map nested multimap
Comments
Post a Comment