public member function
<list>

std::list::push_front

void push_front (const value_type& val);
void push_front (const value_type& val);
void push_front (value_type&& val);
Insert element at beginning
Inserts a new element at the beginning of the list, right before its current first element. The content of val is copied (or moved) to the inserted element.

This effectively increases the container size by one.

Parameters

val
Value to be copied (or moved) to the inserted element.
Member type value_type is the type of the elements in the container, defined in list as an alias of its first template parameter (T).

Return value

none

The storage for the new elements is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// list::push_front
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist (2,100);         // two ints with a value of 100
  mylist.push_front (200);
  mylist.push_front (300);

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';
  return 0;
}


Output:
300 200 100 100 

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is modified.
No existing elements are accessed (although see iterator validity above).

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the container.
If allocator_traits::construct is not supported with val as argument, it causes undefined behavior.

See also