>

1. Pushing the values one-by-one

1
2
3
4
5
6
7
8
9
10
int main() {
vector<int> vec;
vec.push_back(1);
vec.push_back(2);

for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
return 0;
}

2. Using the overloaded constructor of the vector class

1
2
3
4
5
6
7
8
9
10
int main() {
int num_of_ele = 10;
// the overloaded constructor takes the number of
// values and the value itself as parameters.
vector<int> vec(num_of_ele, 1);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
return 0;
}

3. Using arrays

Pass an array to the constructor of the vector class

1
2
3
4
5
6
7
int main() {
vector<int> vec{1,2,3,4,5};
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
return 0;
}

4. Using another, already initialized, vector

Pass another vector to the constructor of the vector

1
2
3
4
5
6
7
8
int main() {
vector<int> vec_1{1,2,3,4,5};
vector<int> vec_2(vec_1.begin(), vec_1.end());
for (int i = 0; i < vec_2.size(); i++) {
cout << vec_2[i] << " ";
}
return 0;
}