asceD's blog

By asceD, history, 6 years ago, In English

Like I want it to be n X m size. How to do it?

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

a.assign(n,vector<pair<ll,ll> >(m));

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it
    vector<vector<ll>> a(3, vector<ll>(5));
    a.resize(3, vector<ll>(3));
    

    Note that in this case vector<vector< ll>> will remain 3x5 size. Be careful with resize.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Can you please explain why?

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        void resize(size_type n, const value_type& Val);

        Val used when n is greater than the current container size. For example:

        vector<vector<ll>> a(2, vector<ll>(2, 0));
        a.resize(3, vector<ll>(3, 1));
        

        a contains:

        0 0

        0 0

        1 1 1

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it
vector<vector<pair<ll,ll>>> a(n, vector<pair<ll,ll>>(m));

or

vector<vector<pair<ll,ll>>> a;
a.assign(n, vector<pair<ll,ll>>(m));