I can do this with a specialized template, I think, for nesting 1,2,3 (the most common cases) by nesting 1,2,3 for loops and referring to types by their names in stl ... but for arbitrary depth, without using a preprocessor, is there any way to do this? Maybe with mpl? Or do I need a preprocessor tool? Now I am doing something like this:
template<typename T, int>
struct MapDump {};
template<typename T >
struct MapDump<T,1>
{
static void dump(const T& map, string file, string header="")
{
if (!header.empty())
cout << header << endl;
for (typename T::const_iterator cIt = map.begin();
cIt != map.end();
++cIt)
cout << cIt->first << "," << cIt->second << endl;
}
};
template<typename T >
struct MapDump<T,2>
{
static void dump(const T& map, string file, string header="")
{
if (!header.empty())
cout << header << endl;
for (typename T::const_iterator it1 = map.begin();
it1 != map.end();
++it1)
for (typename T::mapped_type::const_iterator it2 = it1->second.begin();
it2 != it1->second.end();
++it2)
cout << it1->first << "," << it2->first << "," << it2->second << endl;
}
};
with whom I can call, for example:
map<int, map<int, double> > m;
m[1][1] = 1.0;
m[1][2] = 1.0;
m[2][1] = 2.0;
m[2][2] = 2.0;
MapDump< map<int, map<int, double> >, 2 >::dump(m, "test.csv");
( fstream std:: cout, ). : , , , mapped_type ? , map > 2- , ... 2- ... , , , thsi ( ) .. !