Reputation: 1065
Basic Problem Can you please help me understand how to use a vector of a vector. Take for example vector< vector<int> > help
. I do not understand if it is a vector of ints who each are a vector of ints or if it is a vector of a vector of ints? I also don't understand how to utilize it.
Example Code
vector< vector<int> > test[500];
test[0].emplace_back(1);
cout << test[0][0];
test[50].emplace_back(4);
cout << " " <<test[50][0];
-console-
1 50 //this is not what happens btw, but it is the desired results
Disclaimer I have spent the better part of a morning testing and googling this. Please help :) I did my hw. I can't find any documentation of vectors of a vector. Also I have all the correct libraries and I am using namespace std. I am a noob and i understand that namespaces are bad practice, but its very convient for me right now.
Basically what I want is a set size of a vector filled with each pt being a vector of int. I would rather not go the way of a separate class. Is a vector of a vector of int, the right thing to be looking into?
Thank you :)
Upvotes: 0
Views: 136
Reputation: 4348
A vector
is just a resizable array.
To declare a vector of int
(an array of int), just do:
std::vector<int> vec;
To declare a array in which individual elements are vectors, you do:
std::vector< std::vector<int> > vecarr;
To set the initial size of the vector, you do:
std::vector<int> vec(500);
not std::vector<int> vec[500]
, because this creates an array of 500 std::vectors
. Similarly, std::vector< std::vector<int> > vec[500];
creates a array of 500 vector of vectors.
To skip writing std::
you can say using namespace std
before all this to tell that you're using the std
namespace.
Upvotes: 2
Reputation: 227468
This is a vector of int:
std::vector<int> v;
this is a vector of vectors of int:
std::vector<std::vector<int>> v2;
this is an array of vectors of vectors of ints, which is what you have:
std::vector<std::vector<int>> test[500];
each element of that array is an std::vector<std::vector<int>>
. So test[0]
is one of those.
If you want a vector of 500 default constructed vectors of int, you need
std::vector<std::vector<int>> test(500);
Upvotes: 2
Reputation: 96281
What you have there is an array of vector
-of-vector
which I believe since you're accessing the data with two indexes is not what you wanted.
I believe you may have just typo-ed your constructor initialization:
vector< vector<int> > test(500); // Note () instead of [] here.
This creates a vector-of-vectors, with 500 inner vectors pre-created for you. Then the rest of your code should just work!
Upvotes: 1
Reputation: 169143
test
is an array of 500 vectors of vectors of int. The second line of your example should not even compile here, as you are calling std::vector< std::vector<int> >::emplace_back()
, which expects an argument compatible with std::vector<int>
, and you have provided an int. To clarify:
test
is a std::vector< std::vector<int> >[500]
.test[0]
is a std::vector< std::vector<int> >
.test[0][0]
is a std::vector<int>
.test[0][0][0]
is an int
.(Pedantic C++ developers will note that the latter three are actually references, but I'm omitting that from the type for clarity.)
Upvotes: 2