push_back()
functionThe push_back()
function is used to insert an element at the end of a vector. This function is available in the <vector>
header file.
The push_back()
function accepts one parameter, val
, which specifies the value inserted at the end of the vector.
The push_back()
function does not return anything.
Now, let’s see the code and see how the push_back()
function is used:
#include <iostream>#include <vector>using namespace std;int main() {vector<int> vec = {1,2,3};cout << "Original Vector: ";for(int x: vec)cout << x << " ";vec.push_back(5);vec.push_back(10);cout << "\nVector after pushing some values: ";for(int x: vec)cout << x << " ";return 0;}
push_back()
function and insert some integer values.pop_back()
functionThe pop_back()
function is used to remove the last element from the vector. This function is available in the <vector>
header file.
The pop_back()
function does not accept any parameters.
The pop_back()
function does not return anything.
Now, let’s look at the code and see how to use the pop_back()
function:
#include <iostream>#include <vector>using namespace std;int main() {vector<int> vec = {1,2,3,4,5,6,7};cout << "Original Vector: ";for(int x: vec)cout << x << " ";vec.pop_back();vec.pop_back();cout << "\nVector after popping some values: ";for(int x: vec)cout << x << " ";return 0;}
pop_back()
function and insert some integer values.