471,071 Members | 1,012 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

filling a vector of vector

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
4 13492
D_C
293 100+
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
pra1983
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
3,652 Expert 2GB
Once you build your vector of vectors, you can treat it like a 2D array when traversing and printing.
Oct 4 '07 #4
pra1983
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.

Similar topics

9 posts views Thread by {AGUT2}=IWIK= | last post: by
9 posts views Thread by luigi | last post: by
34 posts views Thread by Adam Hartshorne | last post: by
8 posts views Thread by Ross A. Finlayson | last post: by
3 posts views Thread by crjunk | last post: by
5 posts views Thread by utab | last post: by
1 post views Thread by Carl Banks | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.