Converting 1d vector into 2d vector

This is a discussion on Converting 1d vector into 2d vector within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <vector> int main() { std::vector<std::vector<double> > DV; //2d vector std::vector<double>temp(8,0.0); //1d vector temp[0] = 1; temp[1] ...

1. Converting 1d vector into 2d vector

Code:
```#include <iostream>
#include <vector>

int main()
{
std::vector<std::vector<double> > DV;   //2d vector
std::vector<double>temp(8,0.0);  //1d vector

temp[0] = 1;
temp[1] = 2;
temp[2] = 3;
temp[3] = 4;
temp[4] = 5;
temp[5] = 6;
temp[6] = 7;
temp[7] = 8;

DV.resize(3, temp);

for (int i = 0; i < DV.size(); i++)
{
for (int j = 0; j < DV.size(); j++)
{
std::cout << DV[i][j];
}
}
std::cin.get();
}```
The convertion actually works but it does not give the expected the result. The output should be:

Code:
```1 2 3
4 5 6
7 8```
and it outputs:

Code:
`123123123`
Can anyone help me? Thanks.

2. Code:
`DV.resize(3, temp);`
This will initialize the 2d vector with 3 copys of temp
try
Code:
```      for (int i = 0; i < DV.size(); i++)
{
for (int j = 0; j < DV[i].size(); j++)
{
std::cout << DV[i][j] << " ";
}
std::cout << std::endl;
}```
Kurt

3. It still displays a full row like this:
1 2 3 4 5 6 7 8

4. Originally Posted by Khabz
It still displays a full row like this:
1 2 3 4 5 6 7 8
I know that this will display 3 times the above line. My code was just to make clear what DV actually contains. It is your job to extract the right values.
Kurt

5. Before I posted my reply I changed from >DV.resize(3, temp); to >DV.resize(1, temp);

Ok so if DV contains what I posted above, shouldn't it print out:

Code:
```1 2 3
4 5 6
7 8```
instead of 1 2 3 4 5 7 8 ?

Edited: Oh, I got it. So the vector contains the numbers from 1->8 from DV[0][0->8] instead of DV[0][0->2], DV[1][0->2] and DV[2][0->2]

6. Why do you think it would insert line breaks after 3 elements ?
Kurt