English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and Example of List push_front()

C++ List (List)

C ++  The list push_front() function adds a new element at the beginning of the list. Therefore, the size of the list is increased by one.

The push_front(0) function adds the element 0 at the beginning.

Syntax

Assuming an element is "x":

void push_front(const value_type& x);

Parameter

x: This is the value to be inserted at the beginning of the list.

Return value

It does not return any value.

Example

Let's look at a simple example

#include <iostream>  
#include<list>  
using namespace std;  
int main()
{
    list li={20,30,40,50});
    li.push_front(10);
    list::iterator itr;
    for(itr=li.begin();itr!=li.end();++itr){
        cout<<*itr<<",";
    }
    
    return 0;
}

Output:

10,20,30,40,50

In this example, the push_front() function will insert the element “ 10Insert at the beginning of the list.

C++ List (List)