Vector of vector

Jan 29, 2019 at 2:18pm
hello everyone,

i wanna make a vector of vector with no definite row and column, i mean the row and column must cin by user, or a stream file. okay, how should i build it ?

any advise ?
Jan 29, 2019 at 2:35pm
What have you tried? Post your code showing some effort at solving this basic problem.
Jan 30, 2019 at 5:42am
well, i have no idea about vector of vector that have no certain size, i mean row and column, the only thing that i know, its vector of vector with definite size ( row and column ) by 2 for loop. hope to could convey mean.
Jan 30, 2019 at 5:50am
well, i have no idea about vector of vector that have no certain size, i mean row and column, the only thing that i know, its vector of vector with definite size ( row and column ) by 2 for loop.

for illustration, suppose that we have a txt file as cin ( by ifstream command ), with million of data, the first one is row and the second one as column. the data will update at the same time, its mean we have no definite size row and column. it grows by time. more precisely a 2D dynamic vector. hope to could convey mean.
Last edited on Jan 30, 2019 at 7:27am
Jan 30, 2019 at 12:12pm
Didn't see this post. Anyways also posted here: http://www.cplusplus.com/forum/beginner/249232/

edit: I think I get what you mean. I don't know if this is the same topic as that other post.
You should know the vector itself was designed to be dynamc.

push_back() will come in handy for you by your description.

1
2
3
4
vector < vector<int> > vec;
vec.resize(/* row size */);

vec[0].push_back(/*blah blah*/);

or
1
2
3
4
vector < vector<int> > vec;
vector<int> vec_ele = { /*blah blah*/ };

vec.push_back(vec_ele);


If you have to spontaneously increase the number of columns for every row then use a for-loop.
1
2
for(auto i: vec)
  i.resize(/* new column size */);


But don't resize all rows if they won't have any information because it will be a waste of memory. If you were to print the vectors using a for-loop then you could just skip reach the end of the row.

Last edited on Jan 30, 2019 at 12:21pm
Jan 31, 2019 at 10:55am
Hey Grime, However those posts were almost similar to each other, But that was not me .
Lemme try your suggest. Thanks anyway.
Topic archived. No new replies allowed.