It is impossible in C++ since you have to use iterator to erase.
Here's what I have:
main.ccp
#include <iostream>
#include <vector>
#include "Mastermind.h"
int main()
{
vector<int>* answer=new vector<int>(4);
cout<<"enter an code\n";
for(int i=0; i<4; i++)
{
int k;
cin>>k;
answer->push_back(k);
}
//initalize population
Mastermind a(5);
//set answer
a.setanswer(answer);
a.showpopulation();
a.evaluateblackandwhite();
return 0;
}
Mastermind.h
#include <vector>
#include <string>
using namespace std;
class Mastermind
{
public:
Mastermind(int populationsize);
void showpopulation();
void setanswer(vector<int>* answer);
void evaluateblackandwhite();
void calculatepopulationfitness();
int evaluatepopulation();
int sum(int index);
int randomizenumber();
private:
int populationsize_;
vector<int>* answer_;
vector<vector<int>>* tempanswer_;
vector<int>* fitness_;
vector<vector<int>>* population_;
vector<int>* totalnumberofpegs_;
int* totalnumberofblackpegs_;
int* totalnumberofwhitepegs_;
//iterator through temp array
//iterator through totalnumberofblackpegs
vector<int>::iterator blackpegs;
//iterator through totalnumberofwhitepegs
vector<int>::iterator whitepegs;
//iterate through population
vector<vector<int>>::iterator pop;
//iterate through individual
vector<int>::iterator individual;
vector<vector<string>>* feedback_;
};
/*inline bool operator ==(vector<string> a, string b)
{
if(a==b)
{
return true;
}
else
{
return false;
}
}*/
Mastermind.cpp
#include "Mastermind.h"
#include <iostream>
Mastermind::Mastermind(int populationsize)
{
populationsize_=populationsize;
population_=new vector<vector<int>>(populationsize_,NULL);
for(int i=0; i<populationsize_; i++)
{
for(int j=0; j<4; j++)
{
population_->at(i).push_back(randomizenumber());
}
}
answer_=new vector<int>(4);
fitness_=new vector<int>(populationsize_);
totalnumberofpegs_= new vector<int>(populationsize_);
totalnumberofblackpegs_=new int[populationsize_];
totalnumberofwhitepegs_=new int[populationsize_];
}
void Mastermind::showpopulation()
{
for(pop=population_->begin(); pop!=population_->end(); ++pop)
{
for(individual=(*pop).begin(); individual!=(*pop).end(); ++individual)
{
cout<<*individual<<" ";
}
cout<<endl;
}
}
void Mastermind::setanswer(vector<int>* answer)
{
answer_=answer;
}
void Mastermind::evaluateblackandwhite()
{
//copy answer to temp vector
tempanswer_=new vector<vector<int>>(populationsize_,NULL);
for(int i=0; i<populationsize_; i++)
{
for(int j=0; j<4; j++)
{
tempanswer_->at(i).push_back(answer_->at(j));
}
}
//create feedback array
feedback_=new vector<vector<string>>(populationsize_,NULL);
for(int i=0; i<populationsize_; i++)
{
for(int j=0; j<4; j++)
{
feedback_->at(i).push_back("");
}
}
for(pop=population_->begin(); pop!=population_->end(); pop++)
{
//check for black
for(individual=answer_->begin(); individual!=answer_->end(); individual++)
{
if(answer_[individual]==population_[pop][individual])
{
feedback_[pop][individual].push_back("Match");
for(blackpegs=tempanswer_[pop]->begin(); blackpegs!=tempanswer_[pop]->end(); blackpegs++)
{
//find first instance
if(tempanswer_[pop][blackpegs]==answer_[individual])
{
tempanswer_[pop].erase(blackpegs);
//erase data at that position
break;
}
}
}
}
//check for white(now go back and
//check for tempanswer but in wrong place
/*for(int l=population_->at(i).front(); l<population_->at(i).back(); l++)
{
if(feedback_->at(i).at(l)=="")
{
for(int m=0; m<tempanswer_[i].size(); m++)
{
if(population_[i][l]==tempanswer_[i][m])
{
feedback_[i][l].push_back("Present");
tempanswer_[i].erase(tempanswer_[i].begin()+m);
break;
}
}
}
}
//tally up feedback
for(int a=0; a<feedback_[i].size(); a++)
{
if(feedback_[i][a]=="Match")
{
totalnumberofblackpegs_[i]++;
}
else if(feedback_[i][a]=="Present")
{
totalnumberofwhitepegs_[i]++;
}
else
{
totalnumberofwhitepegs_[i]=totalnumberofwhitepegs_[i];
totalnumberofblackpegs_[i]=totalnumberofblackpegs_[i];
}
}*/
}
/*std::cout<<"after\n";
for(int p=0; p<populationsize_; p++)
{
cout<<totalnumberofblackpegs_[p];
}*/
}
/*void Mastermind::calculatepopulationfitness()
{
//found fitness function online
for(int k=0; k<populationsize_; k++)
{
fitness_[k]=((2*(totalnumberofblackpegs_[k]))+totalnumberofwhitepegs_[k])+sum(k);
}
for(int k=0; k<populationsize_; k++)
{
std::cout<<fitness_[k]<<std::endl;
}
}
int Mastermind::evaluatepopulation()
{
int mxm =(int)fitness_[0];
for (int i=0; i<populationsize_; i++)
{
if (fitness_[i]>mxm)
{
mxm = fitness_[i];
}
}
return mxm;
}
int Mastermind::sum(int index)
{
int sum=0;
for(int i=1; i<totalnumberofpegs_[index]-1; i++)
{
sum=sum+i;
}
return sum;
}*/
int Mastermind::randomizenumber()
{
int d=rand()% 6;
return d;
}
Thanks.