[Solved] C++ vector initialise a vector of agents for a genetic algorithm


The main problem is that you are iterating over collection in which you append during the cycle and even through iterator which is defined as int instead of Agent. Create new vector and push your generated values into this new vector.

Also be aware of using new keyword. You must dealocate that memory later.

Solution:

vector<Agent> population;
vector<Agent> newPopulation;
for (vector<Agent>::iterator i = population.begin(); i != population.end(); ++i) {
    newPopulation.push_back(Agent(generateDna(targetString.size())));
}

2

solved C++ vector initialise a vector of agents for a genetic algorithm