在QMap中嵌套QList

刚接触QT的QMap比较困惑,看这名字以为是二维数组

因为我把QList当作一维数组来用了,事实上也确实可以,但只当一维数组太浪费了。可参考别的资料

QMap<QString, QList<QString> > panelStatistics;
    QList<QString> panelInfo;
    panelInfo.push_back("0102");
    panelInfo.push_back("1253");
    panelInfo.push_back("1256");
    panelStatistics.insert("面板1", panelInfo);

    QMap<QString, QList<QString> >::iterator it;
    for(it = panelStatistics.begin(); it != panelStatistics.end(); ++it)
    {
            QList<QString> tmp = it.value();
            qDebug()<<it.key() <<" "<< tmp[0] <<" "<< tmp[1] <<" "<< tmp[2];
    }

如果你和我一样,非得把QMap用成二维数组

那么你这可这样

//在初始化的时候这样
QMap<int, QList<Qstring> > tableDate;
QList<Qstring> tmp;
for(int i = 0; i < 10; i ++)
{
	tmp.clear();
	tmp.push_back("monday in row"   + QString::number(i));
	tmp.push_back("tuesday in row"  + QString::number(i));
	tmp.push_back("wensday in row"  + QString::number(i));
	tmp.push_back("thursday in row" + QString::number(i));
	tmp.push_back("friday in row"   + QString::number(i));
	tmp.push_back("sateday in row"  + QString::number(i));
	tmp.push_back("sunday in row"   + QString::number(i));
	tableDate.insert(i, tmp);
}
//取值可以这样
QList<Qstring> result;
result = tableDate.value(3);
for(int i = 0; i < reslut.length; i ++)
{
	qDebug() << result[i] + " ";
}

你还可以这样,类似于数据库操作一样

QMapIterator<int, QList<QString> > iter(tableDate);
QList<QString> whatDate;
while(iter.hasNext())
{
	iter.next();
	if(whatDate = iter.value())
	{
		qDebug() << whatDate[0];
		qDebug() << whatDate[1];
		qDebug() << whatDate[2];
		qDebug() << whatDate[3];
	}
}





你可能感兴趣的:(在QMap中嵌套QList)