Thread: Vector of Vectors

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Vector of Vectors

    Hello;

    I need to know how to declare a vector of vectors and how to access.

    vector <int> int_vec;
    #need a vector containing vectors.


    Then I need to do
    int_vec.push_back(integer);
    int_vec.push_back(integer2);
    vector_Containing vectors.push_back(int_vec);
    clear int_vec for use again

    Also, how do I reference the integer now
    ie The integer in container vector position 0 and integer vector position 1.

    Thanks

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    • "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

  3. #3
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    You know the syntax for declaring a vector of int, a vector of vectors of int is only a small step.

    The syntax for declaring a vector is
    Code:
    vector<data_type> vector_name;
    So, in your case, you want a vector of vectors of int. So put vector<int> in for data_type, and we get
    Code:
    vector<vector<int> > vector_name;
    In general, the rule on whitespace in your code is that it's ignored, that is:
    Code:
    cout << someVar;
    
    cout<<someVar;
    
    cout<<                           someVar;
    
    cout
    <<
    someVar;
    are all the same. In the above declaration, the space between the two greater-than signs is neccessary. The compiler will be confused thinking you're using the insertion operator. That doesn't sound so clear re-reading it, so as an example:
    Code:
    vector<vector<int> > vecName;       //this is good
    vector<vector<int>> vecName;        //BAD swanley!  NAUGHTY! {swats you on the nose with a newspaper}
    As far as pushing elements onto the vectors and accessing elements in a vector of vectors, do some more research. Try to see if you can get it on your own, and if not, post what you've tried and why you think it's not working.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM