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. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    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.
    Last edited by Khabz; 04-20-2013 at 01:30 PM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    It still displays a full row like this:
    1 2 3 4 5 6 7 8

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Khabz View Post
    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. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    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]
    Last edited by Khabz; 04-20-2013 at 02:41 PM.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Why do you think it would insert line breaks after 3 elements ?
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting string vector to integer vector
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2010, 05:43 AM
  2. converting part of a vector from string to double/int
    By jakethehake in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2009, 03:19 AM
  3. Converting a program. From Vector to List
    By Swerve in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2009, 03:48 PM
  4. Problem converting std::vector into a reference
    By drrngrvy in forum C++ Programming
    Replies: 18
    Last Post: 10-12-2006, 09:31 PM
  5. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21