Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
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_back

public member function
void push_back ( const T& x );

Add element at the end

Adds a new element at the end of the list, right after its current last 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// list::push_back
#include <iostream>
#include <list>
using namespace std;
int main ()
{
  list<int> mylist;
  int myint;
  cout << "Please enter some integers (enter 0 to end):\n";
  do {
    cin >> myint;
    mylist.push_back (myint);
  } while (myint);
  cout << "mylist stores " << (int) mylist.size() << " numbers.\n";
  return 0;
}

The example uses push_back to add a new element to the container each time a new integer is read.

Complexity

Constant.

See also