c++ - Adding arrays to vector in for loop with random_shuffle -
i'm trying add int-array permutations vector in loop, when leave loop, every vector element same.
task_number = 6; vector<int*> p_0; int* order = new int[task_number]; (int k = 0; k < task_number; k++) order[k] = k; (int = 0; < 3; i++) { random_shuffle(order, order+task_number); cout << endl; p_0.push_back(order); (int j = 0; j < task_number; j++) cout << p_0[i][j] << " "; } ////////////////////////////////////// cout << endl<<endl; std::vector<int*>::iterator it; (it = p_0.begin(); != p_0.end(); ++it) { (int j = 0; j < task_number; j++) cout << (*it)[j] << " "; cout << endl; }
output:
1 3 0 2 5 4 1 4 5 3 2 0 1 3 5 4 0 2 1 3 5 4 0 2 1 3 5 4 0 2 1 3 5 4 0 2
what problem? i'm doing wrong?
you have allocate k number of times integer pointer. if change code follows work
int task_number = 6; vector<int*> p_0; (int = 0; < 3; i++) { int* order = new int[task_number]; (int k = 0; k < task_number; k++) order[k] = k; random_shuffle(order, order+task_number); cout << endl; p_0.push_back(order); (int j = 0; j < task_number; j++) cout << p_0[i][j] << " "; } ////////////////////////////////////// cout << endl<<endl; std::vector<int*>::iterator it; (it = p_0.begin(); != p_0.end(); ++it) { (int j = 0; j < task_number; j++) cout << (*it)[j] << " "; cout << endl; }
Comments
Post a Comment