Qt之Map嵌套对象指针的删除(其他容器也适用)

Method 1:使用qDeleteAll
	h:
	QMap* MapValueForWarn;
    QMap*>* MapCatchForWarn;
    QVector*>*>* VecCatchForWarn;
	cpp:
    VecCatchForWarn = new QVector*>*>();
    for(int i = 0;i<10;i++)
    {
        MapValueForWarn = new QMap();
        MapCatchForWarn = new QMap*>();
        MapValueForWarn->insert(0,QString("17:1%1").arg(i));
        MapCatchForWarn->insert(0,MapValueForWarn);
        VecCatchForWarn->append(MapCatchForWarn);
        qDebug()<value(0)).arg(MapCatchForWarn->value(0)->value(0)).arg(VecCatchForWarn->at(0)->value(0)->value(0));
    }
    qDebug()<value(0)).arg(MapCatchForWarn->value(0)->value(0)).arg(VecCatchForWarn->at(0)->value(0)->value(0));
    qDeleteAll(VecCatchForWarn->begin(),VecCatchForWarn->end()); //Or qDeleteAll(VecCatchForWarn);
	
Method 2:使用QSharedPointer
	h:
	QSharedPointer > SPMapValueForWarn;
    QSharedPointer > > > SPMapCatchForWarn;
    QVector > > > >* SPVecCatchForWarn;
	CPP:
    SPVecCatchForWarn = new QVector > > > >();
    for(int i = 0;i<10;i++)
    {
        SPMapValueForWarn =QSharedPointer>(new QMap);
        SPMapCatchForWarn = QSharedPointer > > >(new QMap > >);
        
        SPMapValueForWarn->insert(0,QString("17:1%1").arg(i));
        SPMapCatchForWarn->insert(0,SPMapValueForWarn);
        SPVecCatchForWarn->append(SPMapCatchForWarn);
        qDebug()<value(0)).arg(SPMapCatchForWarn->value(0)->value(0)).arg(SPVecCatchForWarn->at(0)->value(0)->value(0));
    }
    delete SPVecCatchForWarn;

 

你可能感兴趣的:(Qt学习总结)