Optimal Container for Storing Vectors in C++?

In summary, a vector of vectors is a good way to store vectors because it allows you to access specific elements while also being able to add/remove them.
  • #1
scumbum22
6
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
  • #2
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;
}
 
  • #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:
  • #4
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
 
  • #5
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.
 
  • #6
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;
 
  • #7
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."
 
  • #8
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;
    }
 

1. What is a container of vectors in C++?

A container of vectors in C++ is a data structure that allows for the storage and manipulation of a collection of vectors. It is a part of the Standard Template Library (STL) in C++, and provides efficient and flexible ways to work with vectors.

2. How do I declare a container of vectors in C++?

To declare a container of vectors in C++, you first need to include the vector header file from the STL. Then, you can declare the container using the std::vector template class, specifying the data type of the vectors you want to store within the container. For example, std::vector<int> myContainer; would declare a container of vectors that store integers.

3. What are the advantages of using a container of vectors in C++?

Using a container of vectors in C++ allows for dynamic memory allocation, meaning that the size of the container can change during runtime. It also provides various methods and functions for efficient manipulation and access of the data within the container. Additionally, the use of a template class allows for flexibility in the data types that can be stored within the container.

4. How do I add elements to a container of vectors in C++?

To add elements to a container of vectors in C++, you can use the push_back() method, which adds a new element to the end of the container. You can also use the insert() method to insert an element at a specific position within the container.

5. Can I remove elements from a container of vectors in C++?

Yes, you can remove elements from a container of vectors in C++ using the erase() method, which removes a specific element or a range of elements from the container. You can also use the pop_back() method to remove the last element from the container.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Replies
13
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Linear and Abstract Algebra
Replies
9
Views
571
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
Replies
1
Views
671
  • Programming and Computer Science
Replies
29
Views
5K
Back
Top