Home »
C++ STL
Push and print elements in an integer vector in C++ STL
C++ STL Vector: Here, we are implementing a program in which we are declaring an integer vector, pushing the elements and printing the elements.
Submitted by IncludeHelp, on June 18, 2019
Push and print elements in an integer vector
In this example, we are declaring an integer vector, pushing the elements using a for loop with the counter starting value from 1 to 5 and then printing the value separated by space.
Example
#include <iostream>
#include <vector> //for vector
using namespace std;
int main()
{
// vector for integer elements
vector<int> v1;
// adding 5 elements
for (int i = 1; i <= 5; ++i) {
v1.push_back(i);
}
// print all elements
cout << "vector elements are: ";
for (int i = 0; i < v1.size(); ++i) {
cout << v1[i] << ' ';
}
cout << endl;
return 0;
}
Output
vector elements are: 1 2 3 4 5