In C++, a vector can be initialized with:
- Constructor initialization with no parameters:
vector<int> v;
. This will create an empty vector with size 0. - 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. - Constructor initialization with parameters:
vector<int> v(n, m);
. This will create a vector of size n with all elements initialized to m. - Initialize with curly braces:
vector<int> v = {1, 2, 3};
. This will create a vector of size 3 with elements 1, 2 and 3. - 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}. - Initialize with an iterator:
vector<int> v(v1.begin(), v1.end());
. This will create a vector identical to v1. - Two-dimensional array
//cpp
vector<vector<int>> dp(len, vector<int>(len));
Equivalent to
//java
int[][] dp = new int[len][len]