qDeleteAll
: Qt에서 지원하는 Algorithm 동적할당 삭제
#include <QCoreApplication>
#include <QDebug>
#include <QList>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<Test*> list;
for(int i = 0; i < 10; i++)
{
Test *t = new Test(); //no parent, no smart pointer
t->setObjectName(QString::number(i));
list.append(t);
}
qInfo() << list.at(0);
qDeleteAll(list);
qInfo() << "Count: " << list.count();
// qInfo() << list.at(0); //Will crash
qInfo() << "Clearing...";
list.clear();
qInfo() << "Count: " << list.count();
return a.exec();
}
추가) 그럼 qFill
은 없나?
결론부터 말하면 쓰지말자, 차라리 std::fill을 쓰자
#include <QCoreApplication>
#include <QDebug>
#include <QList>
#include <QtAlgorithms>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<int> list;
list << 1 << 2 << 3 << 4;
// qFill() = do not use
list.fill(9);
qInfo() << list;
list.resize(12);
std::fill(list.begin(),list.end(),-1);
qInfo() << list;
return a.exec();
}
추가2) qSort
는?
역시 std::sort쓰자
#include <QCoreApplication>
#include <QList>
#include <QDebug>
#include <QRandomGenerator>
void randoms(QList<int> *list, int max)
{
list->reserve(max);
for(int i = 0; i < max; i++)
{
int value = QRandomGenerator::global()->bounded(1000);
list->append(value);
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<int> list;
randoms(&list,10);
qInfo() << "Unsorted:" << list;
//qSort - do not use
std::sort(list.begin(),list.end());
qInfo() << "Sorted:" << list;
std::reverse(list.begin(),list.end());
qInfo() << "Reversed:" << list;
return a.exec();
}