Competitive progrsmming

vector<vector<ll>> T(k+1,vector<ll>(k+1));
what does the above statement do? what does vector<ll>(k+1) signifies in above declaration
vector<vector<X>> says to create a vector of vectors of X objects.

T(k+1, vector<X>(k+1)) says to call it T and make it an initial size of k+1 and to fill the k+1 elements of the outer vector with vector<X>'s that all have an initial size of k+1 with the X objects set to their default value (if it has one).
Last edited on
i didnt get it. :(
vector<ll>(k+1) means create a vector. The vector contains objects of type ll. How big is the vector? It is of size (k+1).

http://www.cplusplus.com/reference/vector/vector/vector/

Constructors. This is constructor number 2.
Last edited on
The vector has fill constructor. See http://www.cplusplus.com/reference/vector/vector/vector/
fill constructor
Constructs a container with n elements. Each element is a copy of val.


Simple example of usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Foo val ( arguments ); // construct Foo object with name 'val'. Initialize with arguments
int n = 42; // construct int object with name 'n'. Initialize with value 42

vector<Foo> T ( n, val ); // construct vector of Foo objects with name 'T'

// the vector construction could be done hard way too:
vector<Foo> T; // empty vector
T.reserve( n ); // preallocate
for ( int i=0; i<n; ++i ) {
  T.push_back( val );
}

// or
vector<Foo> T ( n ); // vector with n elements
for ( auto & e : T ) {
  e = val; // copy
}


// or more directly, without n and val:
vector<Foo> T ( 42, Foo( arguments ) );

The Foo( arguments ) in the last version constructs an unnamed, temporary Foo object by calling Foo constructor that accepts arguments. The object is used as argument to call of vector's fill constructor.


In your case
1
2
3
Foo = vector<ll>
arguments = k+1
n = k+1

Your T is essentially a 2D array that has k+1 rows and k+1 columns.
could please tell if i write vector <vector <ll>>T(k+1,k+1)
what will this mean?
Last edited on
Lets try:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
#include <string>

int main()
{
  using ll = std::string;
  using std::vector;
  size_t k {7};
  vector <vector <ll>>T(k+1,k+1);
}

 In function 'int main()':
10:32: error: no matching function for call to 'std::vector<std::vector<std::basic_string<char> > >::vector(size_t, size_t)'
10:32: note: candidates are: ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector();
explicit vector (const allocator_type& alloc);
explicit vector (size_type n, const allocator_type& alloc = allocator_type());
         vector (size_type n, const value_type& val,
                 const allocator_type& alloc = allocator_type());
template <class InputIterator>
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());


You have not told us the type of k. Therefore, we cannot be sure what it actually means. Attempting with size_t k clearly is a syntax error.
Topic archived. No new replies allowed.