[C++] vector initialization

In C++, a vector can be initialized with:

  1. Constructor initialization with no parameters: vector<int> v;. This will create an empty vector with size 0.
  2. Constructor initialization with parameters: vector<int> v(n);. This will create a vector of size n and all its elements will be initialized to 0.
  3. Constructor initialization with parameters: vector<int> v(n, m);. This will create a vector of size n with all elements initialized to m.
  4. Initialize with curly braces: vector<int> v = {1, 2, 3};. This will create a vector of size 3 with elements 1, 2 and 3.
  5. Initialize with curly braces: vector<vector<int>> v = { {1, 2}, {3, 4}};. This will create a two-dimensional vector of size 2, where the first row is {1, 2} and the second row is {3, 4}.
  6. Initialize with an iterator: vector<int> v(v1.begin(), v1.end());. This will create a vector identical to v1.
  7. Two-dimensional array
//cpp
vector<vector<int>> dp(len, vector<int>(len));

Equivalent to

//java
int[][] dp = new int[len][len]

Guess you like

Origin blog.csdn.net/m0_60641871/article/details/131343887