'How to iterate through map of map of map of map of map of map of vector

I am trying to gather all the data from a map of map of map of map of map of map of map of vector without having to have 7 loops in C++.

Here is how the data looks:

Map 1=>Map 1.1=>Map 1.1.1=>Map 1.1.2=>Map 1.1.3=>Map 1.1.4=>Vector 1.1.5=>Elem 1
                                                                        =>Elem 2
       Map 1.2=>Map 1.2.1=>Map 1.2.2=>Map 1.2.3=>Map 1.2.4=>Vector 1.2.5=> Elem 1
                                                                        =>Elem 2
Map 2 =>Map 1.1=>Map 1.1.1=>Map 1.1.2=>Map 1.1.3=>Map 1.1.4=>Vector 1.1.5=>Elem 1
                                                                         =>Elem 2
        Map 1.2=>Map 1.2.1=>Map 1.2.2=>Map 1.2.3=>Map 1.2.4=>Vector 1.2.5=>Elem 1
                                                                         =>Elem 2

So I am trying to collect all the Elem 1, Elem 2 from all the maps into a map.

Can someone help me do this instead of the obvious loop through each map and leading to 7 loops in C++?

Thanks for the help.

(Update: Wow looking back at this after 10 years, what a code nightmare it was trying to fix someone else's legacy code. Hopefully no needs this now :)



Solution 1:[1]

Barring a change of the data type, you're probably stuck with loops-within-loop.

However, I would start by re-writing the maps of maps of maps of .... vector into 1 map of vectors. I would recommend using either boost::tuple or std::tuple (C++0x) to create the class, but you can also define your own, and overload operator < so that it can be used as a map key (or write the comparator)

Using boost::tuple, if you have map<Key1, map< Key2, map<Key3, map<Key4, map<Key5, map<Key6, vector<T> > > > > > > you could rework it into map< boost::tuple< Key1, Key2, Key3, Key4, Key5, Key6 >, vector<T> >.

Solution 2:[2]

If you really must, you could do some template meta programming (for instance using boost mpl), to abstract away the loops. However, as many people have suggested, there is very likely a better solution to your original problem than one that needs 7 nested maps and a vector.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dave S
Solution 2 Janick Bernet