filling a vector of vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gumus
    New Member
    • Sep 2006
    • 1

    filling a vector of vector

    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<flo at> > 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.
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    vec expects you to pass a vector, you are passing it an integer. Define a new vector
    Code:
    std::vector<float> vec_flt;
    for(){
           for() {
                    vec_flt.push_back((float)(i*j));
                  }
         }
    // Now you can push vec_flt into vec
    vec.push_back(vec_flt);
    I must admit, I don't know why you see it necessary to use a vector of vectors.

    Comment

    • pra1983
      New Member
      • Oct 2007
      • 10

      #3
      usage of vectors?

      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.....
      Last edited by pra1983; Oct 4 '07, 07:51 PM. Reason: i would like to send it to the other one who is answering the question ....

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

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

        Comment

        • pra1983
          New Member
          • Oct 2007
          • 10

          #5
          Originally posted by Ganon11
          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......

          Comment

          Working...