English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ vector push_back() usage and example

C++ Vector (Container)

This function adds a new element at the end of the vector (vector).

Syntax

Vector (vector) v, 'k' is the value. The syntax is:

v.push_back(k)

Parameter

k: k is the value to be inserted at the end of the vector (vector).

Return value

This function does not return any value.

The following figure shows how the push_back() function works:

This figure illustrates the insertion of a new element at the end using the push_back() function.

Example

Let's look at a simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<char> v;
	v.push_back('j');
	v.push_back('a');
	v.push_back('v');
	v.push_back('a');
	for(int i = 0; i < v.size(); i++)
	cout << v[i];
	return 0;
}

Output:

java

In this example, the push_back() function inserts the new element into the vector v.

C++ Vector (Container)