C++ 为QMaps格式化qDebug的输出
我目前正在维护一个遗留应用程序。这有很多结构,如:C++ 为QMaps格式化qDebug的输出,c++,qt,formatting,C++,Qt,Formatting,我目前正在维护一个遗留应用程序。这有很多结构,如: QMap<QString, QMap<QString, QMap<QString, QMap<QString, QVariant> > > > Dep; 多亏了四维结构是出了名的难以想象。 但是一些小的环呢 typedef QMap<QString, QVariant> T1; typedef QMap<QString, T1> T2; typedef QMap<Q
QMap<QString, QMap<QString, QMap<QString, QMap<QString, QVariant> > > > Dep;
多亏了四维结构是出了名的难以想象。 但是一些小的环呢
typedef QMap<QString, QVariant> T1;
typedef QMap<QString, T1> T2;
typedef QMap<QString, T2> T3;
foreach( T3 i, dep ) {
cout << "******" << i.key() << "*******" << endl << endl;
foreach ( T2 j, i.value() ) {
cout << j.key() << ":" << endl;
foreach ( T3 k, j.value() ) {
cout << k.key() << "= ";
foreach ( QVariant l, k.value() ) {
cout << l.key() << ": " << l.value() << " ";
}
cout << endl;
}
}
}
当然,使用名称空间std。根据需要添加setw。希望您能理解。这是用于n维的,将使用标准qDebug输出用于已知类型:
template<class NonMap>
struct Print
{
static void print(const QString& tabs, const NonMap& value)
{
qDebug() << tabs << value;
}
};
template <class Key, class ValueType >
struct Print<class QMap<Key, ValueType> >
{
static void print(const QString& tabs, const QMap< Key, ValueType>& map )
{
const QString extraTab = tabs + "\t";
QMapIterator<Key, ValueType> iterator(map);
while(iterator.hasNext())
{
iterator.next();
qDebug() << tabs << iterator.key();
Print<ValueType>::print(extraTab, iterator.value());
}
}
};
template<class Type>
void printMe(const Type& type )
{
Print<Type>::print("", type);
};
模板和递归的出色使用:
template<class NonMap>
struct Print
{
static void print(const QString& tabs, const NonMap& value)
{
qDebug() << tabs << value;
}
};
template <class Key, class ValueType >
struct Print<class QMap<Key, ValueType> >
{
static void print(const QString& tabs, const QMap< Key, ValueType>& map )
{
const QString extraTab = tabs + "\t";
QMapIterator<Key, ValueType> iterator(map);
while(iterator.hasNext())
{
iterator.next();
qDebug() << tabs << iterator.key();
Print<ValueType>::print(extraTab, iterator.value());
}
}
};
template<class Type>
void printMe(const Type& type )
{
Print<Type>::print("", type);
};