By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

filling a vector of vector

P: 1
Hi,
It may be a simple question. But, how can i fill a vector of vector in a loop? Below is what I have tried:

std::vector< std::vector<float> > vec;
for (int i=0; i<n++; i++){
for (int j=0; j<m; j++){
//I tried the below ones. But none works.
vec[i][j]=i*j;
vec.push_back(i*j);
vec[i].push_back(i*j);
vec[i][j].push_back(i*j);
}

Thanks.
Sep 17 '06 #1
Share this Question
Share on Google+
4 Replies


100+
P: 293
D_C
vec expects you to pass a vector, you are passing it an integer. Define a new vector
Expand|Select|Wrap|Line Numbers
  1. std::vector<float> vec_flt;
  2. for(){
  3.        for() {
  4.                 vec_flt.push_back((float)(i*j));
  5.               }
  6.      }
  7. // Now you can push vec_flt into vec
  8. vec.push_back(vec_flt);
I must admit, I don't know why you see it necessary to use a vector of vectors.
Sep 17 '06 #2

P: 10
can u tell me then how to printout the data.... from the same thing vec...

and how to traverse through the vec then....

how to pick an element or check an intermediate element from the vec.....
Oct 4 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Once you build your vector of vectors, you can treat it like a 2D array when traversing and printing.
Oct 4 '07 #4

P: 10
Once you build your vector of vectors, you can treat it like a 2D array when traversing and printing.

actially i am trying to fill the vector with the data from a file. Can u suggest me how to fill the vector.(Is it the same way how i fill the 2D matrix).Can i use a single element of the vector from the middle of the vector at a time.Please let me know how to do that......
Oct 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.