vector of vectors

H

happy

I wanted to make a vector of vectors of int so I tried following :

#include<iostream>
#include<vector>

int main()
{
vector<vector<int> >vec;
for(int i=0;i<2;i++)
{
vector<int>newVec;
vec.push_back(newVec);
for(int j=0;j<3;j++)
{
std::cin>>num;
vec.push_back(num); // Here vec works but newVec.push_back
(num) doesn't work. Why?
}
}
}
 
B

Bo Persson

happy said:
I wanted to make a vector of vectors of int so I tried following :

#include<iostream>
#include<vector>

int main()
{
vector<vector<int> >vec;
for(int i=0;i<2;i++)
{
vector<int>newVec;
vec.push_back(newVec);
for(int j=0;j<3;j++)
{
std::cin>>num;
vec.push_back(num); // Here vec works but
newVec.push_back (num) doesn't work. Why?
}
}
}


It should work just as well (if num was actually declared as an it,
that is).


Bo Persson
 
C

Carlo Milanesi

happy said:
I wanted to make a vector of vectors of int so I tried following :
vector<int>newVec;
vec.push_back(newVec);
for(int j=0;j<3;j++)
{
std::cin>>num;
vec.push_back(num); // Here vec works but newVec.push_back
(num) doesn't work. Why?


I presume you meant: "why if I push an int onto newVec it doesn't end up
onto vec?"
The answer is that when you pushed newVec onto vec, you just made a copy
of it.

If you meant to increase by one the size of vec, there is no need of
newVec. You might simply write:
vec.resize(vec.size() + 1);
Then, if you need an alias to that element, write:
vector<int>& newVec = vec.back();

Instead, if you meant to push onto vec a pointer to vector of ints, you
should have written:
vector<vector<int>* >vec;
for(int i=0;i<2;i++)
{
vector<int>* newVec = new vector<int>;
vec.push_back(newVec);
for(int j=0;j<3;j++)
{
std::cin>>num;
newVec->push_back(num);
or
vec->push_back(num);
 
J

James Kanze

I wanted to make a vector of vectors of int so I tried
following :
#include<iostream>
#include<vector>

int main()
{
vector<vector<int> >vec;
for(int i=0;i<2;i++)
{
vector<int>newVec;
vec.push_back(newVec);
for(int j=0;j<3;j++)
{
std::cin>>num;
vec.push_back(num); // Here vec works but newVec.push_back
// (num) doesn't work. Why?


Both "work" equally well, but they do different things. What
you've written above pushes back on the vector you've inserted
into vec. newVec.push_back pushes back on newVec. After you've
copied the empty vector into vec. And of course, since
newVec immediately goes out of scope, and is destructed, the
push_back don't end up having any visible effect in your code.

Roughly speaking, you have two choices: you can do the push_back
on newVec before inserting it into vec, e.g.:

vector< int > newVec;
for ( int j = 0; j < 3; ++ j ) {
int num;
std::cin >> num;
if ( ! std::cin ) {
// Handle error, probably with a throw or by
// exiting...
}
newVec.push_back( num );
}
vec.push_back( newVec );

, or you can do as you've done. In your case, I find the above
clearer and more logical, but if the vectors being copied are
large, it can cause a performance hit. In such cases, you might
consider using your version, but with vec replaced by
vec.back(). (No point in indexing if what you want is the last
entry.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top