cpp pushfront vector
cpp push_front vector Explanation
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
vec.insert(vec.begin(), 0);
for (int i : vec) {
std::cout << i << " ";
}
return 0;
}
- Include Necessary Libraries: Include the necessary libraries for input/output and vector manipulation.
- Create Vector: Create a vector of integers and initialize it with values 1, 2, 3, 4, and 5.
- Insert Element at the Beginning: Use the
insert
method withvec.begin()
as the first argument and the value 0 as the second argument to insert 0 at the beginning of the vector. - Print Vector Elements: Use a for-each loop to iterate through the elements of the vector and print each element separated by a space.
- Return: End the main function and return 0 to indicate successful execution.