Array of vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajesh V
    New Member
    • Dec 2007
    • 16

    Array of vectors

    We can declare a vector of five 5-element vectors by


    Code:
    vector< vector<int> > v1( 5, vector<int> (5) );


    We can declare an array of five empty vectors by

    Code:
    vector<int> v2[5];

    But how can we declare an array of five 5-element vectors?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, defining a vector of 5 elements just allows for 5 elements but you could add any number of additional elements.

    Second, an array has a fixed number of elements so an array of vectors seems really odd. You should use a vector of vectors. Arrays should be discarded when vector is available. By the time you write the code to manage the array, you will have re-written most of vector and probably have done it in a unique way that can't be used elsewhere.

    Third, all of this should be buried in a class. The class member functions can ensure that only 5 elements are allowed in your vector< vector<int> >.

    Comment

    Working...