Thread: Add element to two demension Vector with no fixed size.

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    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
    3,445
    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
    4,633
    How about:
    Code:
    array.push_back(5); //OK
    matrix.push_back(array);
    Jim

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 01: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, 03:07 PM