In this tutorial, we will learn about push_back() member function of vector in C++.

In C++, the std::vector is a sequential container that works like a dynamic array that can resize itself automatically when an element is added or removed. An important member function of this class is push_back(), which adds a new element to the end of the vector. This function is used extensively to add data to a vector.

Syntax of vector::push_back()

The push_back() member function in C++ has the following syntax:

void push_back(const T& value);
void push_back(T&& value);

In these function prototypes, T represents the type of elements stored in the vector.

Parameters of vector::push_back()

The push_back() function has a single parameter:

  1. value: The value to be added to the end of the vector. It can be a lvalue or rvalue reference, depending on the overloaded version used.

  2. In push_back(const T& value), value is a constant reference to the value being added. This version is used when you add a value that should not be moved or modified.

  3. In push_back(T&& value), value is an rvalue reference. This version allows for moving the value into the vector, which can be more efficient for complex objects.

Return Value of vector::push_back()

The push_back() function does not return a value. It simply adds the new element to the end of the vector.

Example of vector::push_back()

First, we will create a vector of integers and then add three elements to the vector by calling the push_back function. We will pass each element into the push_back function, which will add them to the end of the vector.

For example, this is our vector:

std::vector<int> numbers;

If you want to add the first element to the vector, you can call the push_back function and pass 10 to it. Like this,

numbers.push_back(10);

The contents of the vector will look like this:

[10]

Next, we will call the push_back function again, this time passing 11. Like this,

numbers.push_back(11);

The contents of the vector will be as follows:

[10, 11]

After that, we can once again call the push_back function and pass 12. Like this,

numbers.push_back(12);

It will add the element at the end of the vector, and the vector will look like this:

[10, 11, 12]

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers;

    // Adding elements to the vector
    numbers.push_back(10);
    numbers.push_back(11);
    numbers.push_back(12);

    std::cout << "Vector elements: ";
    for (int element : numbers)
    {
        std::cout << element << " ";
    }

    return 0;
}

Output

Vector elements: 10 11 12

In this code, we added three elements into a vector numbers by passing them to push_back() function one by one. After that we printed the contents of vector using a for loop

Summary

Today, we learned how to use the push_back() function of vector in C++.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.