vector::push_back() function in C++

In C++, the vector class provides a member function push_back(). It accepts an element as an argument, and adds that element to the end of the vector. Basically it increases the size of the vector by one. As per vector’s internal implementation, sometimes it might be possible the due to addition of an element at the end of vector, a relocation of all the elements happens inside the vector.

Table Of Contents

Syntax of vector::push_back()

Let’s see the syntax of the vector::push_back() function,

void push_back (const value_type& val);
void push_back (value_type&& val);

Parameters:

val: The value that need to be added at the end of the vector. The type of the value should match the type of the vector.

Advertisements

Returns: None

Let’s see an example,

Example 1 of vector::push_back()

Here, we have a vector that contains the five integers. Now we will add an another element, that is an integer at the end of the vector using push_back() function.

#include <iostream>
#include <vector>

int main ()
{
    // create a vector, and initialize with five integers
    std::vector<int> vectorObj {11, 12, 13, 14, 15};

    // Append an element at the end of vectors
    vectorObj.push_back(22);

    // print all elements in vector
    for(const int& num: vectorObj){
        std::cout<< num << ", ";
    }
    std::cout<< std::endl;

    return 0;
}

Output:

11, 12, 13, 14, 15, 22,

We called the function push_back() and passed a new element into it. It added the element at the end of the vector.

Example 2 of vector::push_back()

Here, we have a vector that contains the five strings. Now we will add an another element, that is another string at the end of the vector using push_back() function.

#include <iostream>
#include <vector>
#include <string>

int main ()
{
    // create a vector, and initialize with five strings
    std::vector<std::string> vectorObj {"Hi", "this", "is", "a", "sample"};

    // Append an element at the end of vectors
    vectorObj.push_back("text");

    // print all elements in vector
    for(const std::string& elem: vectorObj){
        std::cout<< elem << " ";
    }
    std::cout<< std::endl;

    return 0;
}

Output:

Hi this is a sample text 

Here, we added a new string at the end of the vector.

Important note about vector::push_back() function

When we create a vector, then it allocates some memory internally to store the elements. After that we can keep on adding elements to the vector. But when it reaches the point where the allocated memory is not enough; then it allocates a bigger chunk of memory and copies the existing element to this new memory. This is called relocation. Therefore, with the push_back() function, we can add an element at the end of the vector, but it might be possible that any particular call of push_back() function, a complete relocation happens. This relocation, is a heavy operation. So we always need to keep this in mind while calling the push_back() function.

Summary

We learned how to use push_back() function of vector in C++. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top