Add element to two demension Vector with no fixed size.

This is a discussion on Add element to two demension Vector with no fixed size. within the C++ Programming forums, part of the General Programming Boards category; My question is very easy if this vector has a fixed size. for example: Code: vector< vector<int> > matrix(5,vector<int>(5,0)); matrix[4][4]=2; ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Add element to two demension Vector with no fixed size.

    My question is very easy if this vector has a fixed size. for example:
    Code:
    vector< vector<int> > matrix(5,vector<int>(5,0));
    matrix[4][4]=2;
    matrix[3][2]=1;
    But that is not obvious when you don't fixed size for vector. You cannot use push_back like one demension array. This is my example:
    Code:
      vector< vector<int> >matrix;
      vector<int>   array;
      array.push_back(5); //OK
      matrix.push_back(5); //NOT OK. So How can.
    So, that's my problem. Who know how to code it, help me please.
    thanks for ton

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,288
    when adding items to matrix, you have to push_back a vector<int>, not just an int. so in your example, you might say matrix.push_back(array) instead of matrix.push_back(5).

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,690
    How about:
    Code:
    array.push_back(5); //OK
    matrix.push_back(array);
    Jim

  4. #4
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Oh, thanks for yours help But, I think above solution has a weak point: you must process array vector first before you add it too matrix. So, two demension vector will not as flexible as two demension array. huh :-?

    thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,394
    Quote Originally Posted by hqt
    But, I think above solution has a weak point: you must process array vector first before you add it too matrix. So, two demension vector will not as flexible as two demension array. huh :-?
    It is you who chose to create the vector named array (which is a poor name given std::array). I can choose otherwise:
    Code:
    vector< vector<int> > matrix;
    matrix.push_back(vector<int>(1, 5));
    That said, if you have more elements for the inner vector that arbitrarily differ in value, then you may need to do more than just the above.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Actually, the problem arises from the fact that you do not specify whether you want to push back a new column or a new row.
    What does matrix.push_back(5) mean? Row? Or column?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  2. Size of fixed size object?
    By shwetha_siddu in forum C Programming
    Replies: 2
    Last Post: 04-08-2009, 02:39 AM
  3. Replies: 1
    Last Post: 02-28-2008, 12:30 PM
  4. Use vector instead of fixed-size array
    By clegs in forum C++ Programming
    Replies: 19
    Last Post: 09-16-2007, 09:00 PM
  5. Fixed window size
    By The Brain in forum Windows Programming
    Replies: 5
    Last Post: 02-23-2006, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21