Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Container of vectors in C++

  1. Jun 24, 2011 #1
    What is the best container to store vectors in for C++? Is it possible to declare a vector whose elements are vectors? Or is there some other sort of container that can be used?

    I was thinking of using a vector of vectors because I need to be able to access specific elements while also being able to add/remove them.

    Any help would be appreciated!
     
  2. jcsd
  3. Jun 24, 2011 #2

    jtbell

    User Avatar

    Staff: Mentor

    Yes, you can have a vector of vectors, or a list of vectors, or a vector of lists, or probably most any other combination of the standard containers. Example:

    Code (Text):

    #include <iostream>
    #include <iomanip>
    #include <vector>

    using namespace std;

    int main ()
    {
        const int numrows = 10;
        const int numcols = 10;
        vector<vector<double> > mymatrix(numrows, vector<double>(numcols));

    // mymatrix is a vector of numrows elements.  Each of those elements is
    // a vector of numcols doubles.  Notice the extra blank space in
    // <double> >.  If that space is missing, the compiler interprets >> as a
    // stream extraction operator as in cin >> number, which doesn't make
    // sense here, and generates a compiler error message.

        for (int row=0; row<numrows; row++)
        {
            for (int col=0; col<numcols; col++)
            {
                mymatrix[row][col] = row*col;
            }
        }

        for (int row=0; row<numrows; row++)
        {
            for (int col=0; col<numcols; col++)
            {
                cout << setw(5) << mymatrix[row][col];
            }
            cout << endl;
        }

        return 0;
    }
     
     
  4. Jun 24, 2011 #3
    Oh I see! Thank you very much.

    I won't be able to declare the vectors that will be within the vector. Instead I need to do use push_back or some similar operation to add the vectors to form a matrix.

    How would you go about using push_back for a matrix? Will it add on columns or rows?,
     
    Last edited: Jun 24, 2011
  5. Jun 24, 2011 #4

    Borek

    User Avatar

    Staff: Mentor

    I can be wrong, but I think thread title is slightly misleading - question is not about C++, but about STL.

    Edit: oh, and checking what setw means is faster than typing the question: http://lmgtfy.com/?q=setw
     
  6. Jun 24, 2011 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Or any of the other ways of filling a vector -- such as creating it or resizing it to the correct length then assigning to its entries.


    You don't. You use push_back to add a <thing> to a vector of <thing>s. (where <thing> here is a vector)

    What a vector of vector means in terms of matrices depends on how you are using your vector of vectors to represent the matrix.

    For example, if you are interpreting the elements of v as rows of some matrix (so, e.g., v.front() and v[0] are both references for the top row), then v.push_back adds a new row.
     
  7. Jun 24, 2011 #6

    jtbell

    User Avatar

    Staff: Mentor

    To see how to use push_back with a vector of vectors, insert the following code in my program, immediately before the "return 0;":

    Code (Text):

    // Now append an empty row to the end of the matrix:

        mymatrix.push_back(vector<double>());

    // This new row should be number 10.  Append one number to it:

        mymatrix[10].push_back(3.14159);

    // This number should be at row 10, column 0:

        cout << mymatrix[10][0] << endl;
     
     
  8. Jun 24, 2011 #7

    jtbell

    User Avatar

    Staff: Mentor

    What was once called the STL is now part of standard C++, more precisely the "C++ standard library."
     
  9. Jun 24, 2011 #8

    jtbell

    User Avatar

    Staff: Mentor

    Now that I've got this stuff on my mind, I might as well note that if you use push_back to build the vector of vectors, the rows can be different lengths. One way to traverse such a "ragged matrix" correctly:

    Code (Text):

    // traversing a "ragged matrix":

        for (int row=0; row<mymatrix.size(); row++)
        {
            for (int col=0; col<mymatrix[row].size(); col++)
            {
                cout << setw(5) << mymatrix[row][col];
            }
            cout << endl;
        }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Container of vectors in C++
  1. C++ Vectors (Replies: 1)

Loading...