471,071 Members | 1,012 Online

# 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