Thread: dynamic array of vectors

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    dynamic array of vectors

    Hi,
    I would like to create a dynamic array of vectors. This is my code so far:
    Code:
    std::vector<int> vectorInput; /*A vector containing the input from the file*/
    std::vector<int> *vectorArray; /*A dynamic array containg the vector for the different nodes*/
    
    vectorArray = new std::vector<int>[numberOfNodes];
    I would then like to read a file and put the different lines in different vectors and store the pointer to the vectors in the array.

    Since I do not know the number of lines beforehand, I was wondering how to dynamically create the vectors. Thanks
    Amish

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why not go with a vector of vectors?
    Code:
    std::vector<std::vector<int> > vectorArray;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    don't forget you will need to populate a vector<int> before you pushback into your vector of vectors:

    Code:
    vector<vector<int> > 2D_vector;
    vector<int> 1D_vector;
    
    1D_vector.push_back(42);
    1D_vector.push_back(98);
    1D_vector.push_back(22);
    
    2D_vector.push_back(1D_vector);
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    I fill up the 1D vector the first time around with data from the first line of a file and push it onto the 2D vector but what happens when I read a second line and I want to push the data onto a new 1D vector and push that 1D vector onto the 2D vector. I cannot use the same 1D_vector object else the old data is going to be overwritten. Thanks
    Amish
    Last edited by axr0284; 02-24-2006 at 10:41 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I cannot use the same 1D_vector object else the old data is going to be overwritten.
    Why do you care?
    Last edited by 7stud; 02-24-2006 at 10:47 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by axr0284
    I fill up the 1D array the first time around with data from the first line of a file and push it onto the 2D array but what happens when I read a second line and I want to push the data onto a new 1D vector and push that 1D vector onto the 2D vector. I cannot use the same 1D_vector object else the old data is going to be overwritten.
    Clear the 1D vector (with an invalid identifier name mentioned by The Brain) after pushing it; each 1D vector is saved when you push it back.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Why do you care?
    Just to clarify that: vectors make copies of the data. When you push_back(theData), the Data is copied, and the copy is inserted into the back of the vector. So, when you push_back() the 1d vector into your 2d vector, the 1d vector is copied, and the copy is inserted into the 2d vector. Thereafter, anything you do to the 1d vector has no effect on what's in the 2d vector.
    Last edited by 7stud; 02-24-2006 at 10:59 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I started out with good intentions for this post, but...
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <iterator>
    using namespace std;
    
    int main()
    {
       vector<vector<int> > my2Dvector;
       ifstream file("file.txt");
       string line;
       while ( getline(file, line) )
       {
          istringstream iss(line);
          int value;
          vector<int> my1Dvector;
          while ( iss >> value )
          {
             my1Dvector.push_back(value);
          }
          my2Dvector.push_back(my1Dvector);
       }
       // (horrible, but an example)
       for ( vector<vector<int> >::size_type i = 0; i < my2Dvector.size(); ++i )
       {
          for ( vector<int>::size_type j = 0; j < my2Dvector[i].size(); ++j )
          {
             cout << my2Dvector[i][j] << ' ';
          }
          cout << '\n';
       }
    }
    
    /* file.txt
    1 2 3
    4 5 6 7
    8 9
    */
    
    /* my output
    1 2 3 
    4 5 6 7 
    8 9 
    */
    ...it ended up like this.
    Last edited by Dave_Sinkula; 02-24-2006 at 11:24 PM. Reason: Exchanged int for <T>size_type.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    interesting. I did not know vectors made copy of the data. I thought they used pointers or something. Thanks for the help everybody,
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of vectors
    By earth_angel in forum C++ Programming
    Replies: 7
    Last Post: 07-12-2005, 09:46 AM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM