我有一个名为Individual的类的指针向量。在所述类中,我有一个int数组和一个int向量。我已经尝试了三种不同的方法来根据我在stackoverflow上看到的答案清除向量,但似乎没有工作。他们是:
方法1:
int populationSize = (int) this->population.size();
this->population.clear();
this->population.shrink_to_fit();
this->population = *new vector<Individual*>(populationSize, NULL);
方法2:
int populationSize = (int) this->population.size();
vector<Individual*>(this->population).swap(this->population);
this->population = *new vector<Individual*>(populationSize, NULL);
方法3:
int populationSize = (int) this->population.size();
for (std::vector< Individual* >::iterator it = this->population.begin() ; it != this->population.end(); ++it)
{
delete *it;
}
this->population.clear();
this->population = *new vector<Individual*>(populationSize, NULL);
这些似乎都不起作用,我真的需要得到这个代码。在此先感谢您的帮助。
Ps:这是Individual类的属性和析构函数方法,虽然我很确定这不是问题所在。
//Attributes
int fitness, limit, *sudokuBoard;
vector<int> fixedPositions;
static Helper h;
//Destructor
~Individual(void) {delete [] this->sudokuBoard; this->fixedPositions.clear();};
编辑: 这不是一个重复的问题,因为提供的信息不能回答我的问题。