Optimal Container for Storing Vectors in C++?

  • Context: C/C++ 
  • Thread starter Thread starter scumbum22
  • Start date Start date
  • Tags Tags
    C++ Container Vectors
Click For Summary

Discussion Overview

The discussion revolves around the best container for storing vectors in C++, specifically focusing on the use of a vector of vectors. Participants explore the capabilities of different standard containers and how to manipulate them for matrix-like structures.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant asks if it is possible to declare a vector whose elements are vectors and suggests using a vector of vectors for accessing specific elements while allowing for addition and removal.
  • Another participant confirms that a vector of vectors is possible, along with other combinations of standard containers, and provides an example of initializing a matrix using a vector of vectors.
  • A participant expresses the need to use push_back to add vectors to form a matrix and inquires about how push_back would affect the structure (rows or columns).
  • One participant suggests that the original question may be misleading, arguing that it pertains more to the Standard Template Library (STL) than to C++ itself.
  • Another participant clarifies that push_back is used to add a vector to a vector of vectors and explains how this relates to the representation of matrices.
  • A later reply provides code to demonstrate how to use push_back to append rows to a matrix represented by a vector of vectors.
  • One participant notes that using push_back can lead to rows of different lengths, introducing the concept of a "ragged matrix" and how to traverse it correctly.

Areas of Agreement / Disagreement

Participants generally agree on the feasibility of using a vector of vectors and the functionality of push_back, but there are differing views on the implications of the original question regarding C++ versus STL. The discussion remains unresolved regarding the best practices for using these containers.

Contextual Notes

Some participants mention limitations regarding the interpretation of matrix representations and the potential for varying row lengths in a vector of vectors.

scumbum22
Messages
6
Reaction score
0
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!
 
Technology news on Phys.org
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:
#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;
}
 
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:
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
 
scumbum22 said:
Instead I need to do use push_back or some similar operation to add the vectors to form a matrix.
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.


How would you go about using push_back for a matrix?
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.
 
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:
// 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;
 
Borek said:
I can be wrong, but I think thread title is slightly misleading - question is not about C++, but about STL.

What was once called the STL is now part of standard C++, more precisely the "C++ standard library."
 
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:
// 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;
    }
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
13
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
8K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K