cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : list : push_front
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
list
comparison operators
list::list
list::~list
member functions:
· list::assign
· list::back
· list::begin
· list::clear
· list::empty
· list::end
· list::erase
· list::front
· list::get_allocator
· list::insert
· list::max_size
· list::merge
· list::operator=
· list::pop_back
· list::pop_front
· list::push_back
· list::push_front
· list::rbegin
· list::remove
· list::remove_if
· list::rend
· list::resize
· list::reverse
· list::size
· list::sort
· list::splice
· list::swap
· list::unique

-

list::push_front public member function
void push_front ( const T& x );

Insert element at beginning

Inserts a new element at the beginning of the list, right before its current first element. The content of this new element is initialized to a copy of x.

This effectively increases the list size by one.

Parameters

x
Value to be copied to the new element.
T is the first template parameter (the type of the elements stored in the container).

Return value

none

The storage for the new element is allocated using Allocator::allocate(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

Example

// list::push_front
#include <iostream>
#include <list>
using namespace std;

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

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

  cout << endl;
  return 0;
}

Output:

300 200 100 100 

Complexity

Constant.

See also

list::push_back Add element at the end (public member function)
list::pop_front Delete first element (public member function)
list::insert Insert elements (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us