Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

list push_back() function in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 17 Jan, 2022

The list:push_back() function in C++ STL is used to add a new element to an existing list container. It takes the element to be added as a parameter and adds it to the list container.
Syntax: 
 

list_name.push_back(value) 

Parameters: This function accepts a single parameter which is mandatory value. This refers to the element needed to be added to the list, list_name.
Return Value: The return type of this function is void and it does not returns any value. 
Below program illustrates the list::push_back() function. 
 

CPP




// CPP program to illustrate the
// list::push_back() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialization of list
    list<int> demo_list;
 
    // initial size of list
    cout << "Initial Size of the list: "
         << demo_list.size() << endl;
 
    // Adding elements to the list
    // using push_back function
    demo_list.push_back(10);
    demo_list.push_back(20);
    demo_list.push_back(30);
 
    // Size of list after adding
    // some elements
    cout << "Size of list after adding three "
         << "elements: " << demo_list.size();
 
    return 0;
}


Output: 

Initial Size of the list: 0
Size of list after adding three elements: 3

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!