I have the following 2D array
54 65 22 34 11
43 12 44 22
I have the following vector syntax:
vector<vector<int>> data;
vector<int> row_1 = data[0]; // return 54 65 22 34 11
vector<int> row_2 = data[1]; // return 43 12 44 22
Is there a way I can make the row_1 and row_2 vector into this:
int *row_1 = new int[5] // return 54 65 22 34 11
int *row_2 = new int[4] // return 43 12 44 22
Thanks.
Ensure the vector is the same as the new int array you are trying to store it as. Probably best to allocate it with row_1 vector size:
vector<int> row_1_vec = data[0]; // return 54 65 22 34 11
vector<int> row_2_vec = data[1]; // return 43 12 44 22
int* row_1 = new int[row_1_vec.size()];
int* row_2 = new int[row_2_vec.size()];
Now traverse through the vector:
for(int val = 0; val < row_1_vec.size(); val++)
{
row_1[val] = row_1_vec[val];
}
for(int val = 0; val < row_2_vec.size(); val++)
{
row_2[val] = row_2_vec[val];
}
Make sure when you are done using row_1 and row_2 to delete it by doing
delete[] row_1;
delete[] row_2;