Ich habe folgendes Skript, das alle möglichen Punkte in einer bestimmten Basis erstellt:Vector Größe nicht als variable Erhöhung hinzugefügt werden
int main(){
int base;
cout << "Enter Base: ";
cin >> base;
int dimension;
cout << "Enter Dimension: ";
cin >> dimension;
//This creates all possible numbers
vector<double> dist;
dist.reserve(base);
for(int k=0; k<base; k++){
dist[k] = (-1.0+k*2.0/(base-1.0));
}
vector< vector<double> > points;
int perms = 1;
for(int i=0; i<dimension; i++){
perms *= base;
} // base^dimension
points.reserve(perms);
vector<double> stand;
stand.reserve(dimension);
// Defined later
getPermutations(dist, base, stand, dimension, 0, points);
for(int i=0; i<points.size(); i++){ //DOESN'T DO ANYTHING BECAUSE SIZE IS 0
cout << '(';
for(int j=0; j<points[i].size(); j++){
cout << points[i][j] << ',';
}
cout << ')' << endl;
}
return 0;
}
Es wird nichts tun, weil die Größe Funktion nur erhöht, wenn ich den push_back verwenden() -Funktion statt Indizierung. Ich habe die Indizierung zu verwenden, da die Permutationen unten Funktion:
void getPermutations(vector<double>& arr, int size,
vector<double>& data,int dimension,
int index, vector< vector<double> >& combs){
int i;
//stop recursion condition
if(index == dimension){
combs.push_back(data);
}
else{
for(i = 0; i < size; i++){
data.at(index) = arr.at(i);
getPermutations(arr, size,data,
dimension,index+1, combs);
}
}
}
Ich verstehe nicht, warum die Vektorgrößen Null sind und Fehler immer wieder auftauchen sagen:
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Es scheint, dass Sie reserve() mit resize() verwirren. –
Ändere dies: 'dist [k] = (-1.0 + k * 2.0/(Basis-1.0));' zu diesem: 'dist.at (k) = (-1.0 + k * 2.0/(Basis-1.0)); '. Sobald Sie das tun, sollte das Problem offensichtlich sein. – PaulMcKenzie