PDA

View Full Version : Container of vectors in C++


scumbum22
Jun24-11, 11:45 AM
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!

jtbell
Jun24-11, 01:05 PM
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:


#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;
}

scumbum22
Jun24-11, 01:24 PM
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?,

Borek
Jun24-11, 01:35 PM
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

Hurkyl
Jun24-11, 01:51 PM
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.

jtbell
Jun24-11, 03:57 PM
To see how to use push_back with a vector of vectors, insert the following code in my program, immediately before the "return 0;":


// 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;

jtbell
Jun24-11, 04:32 PM
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."

jtbell
Jun24-11, 04:39 PM
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:


// 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;
}