I create a class, which creates a new list of pointers to objects:
FeatureSet::FeatureSet() {
this->featuresList = new list<HaarFeature*>;
globalCounter = 0;
}
Now I would like to delete all object from the list, and then delete the pointer to the list. My code is:
FeatureSet::~FeatureSet() {
for (list<HaarFeature*>::iterator it = featuresList->begin(); it != featuresList->end(); it++) {
delete *it;
}
delete featuresList; // this take a long time (more than half a minute)
}
My question is what is the best method to solve this problem? Does my approach is correct? Unfortunately, currently the last operation (deleting a pointer to the list) takes about half minute. List has about 250000 objects.
OK. So I performed some research. I just have tested four solutions. Results are presented in the table below: (I can’t put an image, please click the link)
Comparing an efficiency of list and vector containers
As you can see from the table, vector containing objects is the fastest solution. It is also true that storing pointers to objects is much slower than containing real objects.
SUMMARY:
Answering to question asked by @Emile Cormier – Why I would like to use pointer to container? This ensure me an access to vector/list elements even after deletion of parent class object. Please consider this part of code:
When I don’t use a pointer to vector (case B), I although can access objects from vector after deletion of “B” class object BUT in reality I use a local copy of returned vector. Using a pointer to vector (case A) allows me to get an access to vector objects even after deletion of “A” class object, when I don’t call clear() method in destructor manually of course.
Finally – thanks everyone for help. I think that my problem has been solved.