how to resize a 2D vector?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tinie
    New Member
    • Jan 2007
    • 4

    how to resize a 2D vector?

    How can I resize a 2D vector of int? initially i have declared vector<vector<i nt> > value;
    I saw that
    Code:
    void resize(n, t = T())
    should be used, but I can't seem to understand how it works. I want to initialize the added elements initialed to some integer value, say 1. how can I do that?

    Thanks for the help.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by tinie
    How can I resize a 2D vector of int? initially i have declared vector<vector<i nt> > value;
    I saw that
    Code:
    void resize(n, t = T())
    should be used, but I can't seem to understand how it works. I want to initialize the added elements initialed to some integer value, say 1. how can I do that?

    Thanks for the help.
    you need to resize the vector and its elements, e.g.
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
      vector < vector < int > > value(5, vector <int>(10,0));
      cout << "value capacity " << value.capacity() << endl;
      for(int i=0; i<value.capacity();i++)
         cout << "  elment capacity " << (value.at(i)).capacity() << endl;
      // resize value
      value.resize(10, vector <int>(10,0));
      cout << "value capacity " <<  value.capacity() << endl;
      for(int i=0; i<value.capacity();i++)
         cout << "  elment capacity " << (value.at(i)).capacity() << endl;
      // resize element
      for(int i=0; i<value.capacity();i++)
         (value.at(i)).resize(20, 5);
      cout << "value capacity " <<  value.capacity() << endl;
      for(int i=0; i<value.capacity();i++)
         cout << "  elment capacity " << (value.at(i)).capacity() << endl;
      cin.get();
    }

    Comment

    Working...