list::push_back() method

Lists are sequence containers available in the C++ Standard Library. They allow non-contiguous memory allocation. List containers are implemented as doubly-linked lists. Compared to other sequence containers (eg: arrays, vectors etc.), lists have a better performance when it comes to inserting, moving or deleting elements. The list::push_back() is a method available in the STL which adds a new element at the end of the list container, after its current last element. It effectively increases the container size by one.

Syntax:

list_name.push_back(value);

Parameters: The list::push_back() method accepts a single parameter:
•Element to be inserted to the list
Return value: none

Example of list::push_back() method

#include <iostream>
#include <list>

using namespace std;

int main()
{
    list<int> L;
    L.push_back(3);
    L.push_back(4);
    L.push_back(5);
    L.push_back(6);

    cout<<"List contains:  \n";

    for( list<int>::iterator it = L.begin(); it != L.end(); ++it)
    {
        cout<<*it<<" <--> ";
    }
    cout<<"NULL";
}

Output:

List contains:
3 <--> 4 <--> 5 <--> 6 <--> NULL

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

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

Scroll to Top