C++ Standard Library C++ STL Library

C++ <list> - push_back() Function



The C++ list::push_back function is used to add a new element at the end of the list. Addition of new element always occurs after its current last element and every addition results into increasing the container size by one.

Syntax

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

Parameters

val Specify the new element which need to be added in the list.

Return Value

None.

Time Complexity

Constant i.e, Θ(1)

Example:

In the example below, the list::push_back function is used to add new elements in the list called MyList.

#include <iostream>
#include <list>
using namespace std;
 
int main (){
  list<int> MyList;
  list<int>::iterator it;

  //add new elements in the list
  MyList.push_back(10);
  MyList.push_back(20);
  MyList.push_back(30);
  MyList.push_back(40);
  MyList.push_back(50);


  cout<<"MyList contains:";
  for(it = MyList.begin(); it != MyList.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

MyList contains: 10 20 30 40 50

❮ C++ <list> Library