To create a vector of integers in C++, you can use the std::vector class and the push_back method to add integers to the vector.
In this example,
header to use the std::vector
class.int_vector
of type std::vector
.push_back
method to add integers one by one to the vector.#include <vector>
#include <iostream>
int main() {
std::vector<int> int_vector;
int_vector.push_back(1);
int_vector.push_back(2);
int_vector.push_back(3);
int_vector.push_back(4);
int_vector.push_back(5);
for (int num : int_vector) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
1 2 3 4 5
In this example,
{}
with a list of integers inside to initialize a vector.int_vector
.#include <vector>
#include <iostream>
int main() {
std::vector<int> int_vector = {1, 2, 3, 4, 5};
for (int num : int_vector) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
1 2 3 4 5
In this tutorial, we learned How to create a Vector of Integers in C++ language with well detailed examples.