[]
or at()
.
<list>
header.
list<T> l
.
list<T> l(n, v)
.
v
is optional with a T
default constructor.
list<T> l2(l1)
.
l1
list elements are copied into l2
, not shared with
l2
.
list<T> l(start, end)
.
start
and end
iterators can point into
any container, not just a list.
l
, not shared with l
.
[]
or .at()
.
front()
and
back()
.
size()
and max_size()
member functions.
reserve()
and capacity()
member functions.
l.push_back(v)
, l.pop_back()
,
l.push_front(v)
, and l.pop_front()
member
functions.
l.resize(n)
changes the number of elements in l
to n
.
l.resize(n, v)
changes the number of elements in l
to n
.
v
are added if necessary.
insert(i, v)
inserts the value v
immediately
before (to the left of) the value referenced by the
iterator i
.
v
.
insert(i, n, v)
inserts n
copies of the
value v
immediately before (just to the left of)
the value referenced by the iterator i
insert(i, start, end)
inserts the values denoted
by the iterator range start,end
immediately
before the value referenced by the iterator i
.
erase(i)
deletes the value referenced by the
iterator i
.
erase(s, e)
deletes the values denoted by the
iterator range (s, e)
remove(val)
removes all elements with the given
value.
remove_if(p)
removes all elements for which p()
returns true.
clear()
removes all elements.
l1.splice(i, l2)
inserts the list l2
before
the list l1
value referenced by iterator i
.
l2
is empty after the splice.
l1
and l2
must be different.
l1.splice(i1, l2, i2)
inserts the l2
value
referenced by the iterator i2
before the list
l1
value referenced by iterator i1
.
i2
is deleted from
l2
.
l1
and l2
need not be different.
l1.splice(i1, l2, 2, e)
inserts the l2
values
referenced by the iterator range (s, e)
before the
list l1
value referenced by iterator i1
.
(s, e)
are deleted from
l2
.
l1
and l2
need not be different, but the
iterator range may not contain i1
.
+
, -
, +=
, or
-=
.
-
.
l.sort()
sorts l
in O(n2) time.
l.sort(c)
sorts l
using the comparison c
.
unique()
replaces consecutive runs of a value with
one copy of the value.
unique(p)
replaces consecutive runs with a single
value using p
to determine when adjacent values are
equal
l1.merge(l2)
, l1.merge(l2, c)
merges l2
into l1
, using c
; l2
is empty after.
l1
and l2
should be sorted; the merged
list is sorted too.
vector | list | |
---|---|---|
insert and erase | O(n) | O(1) |
push_front() |
O(n)1 | O(1) |
find() 3 |
O(n) | O(n) |
C[i] | O(1) | O(n)2 |
iterator | random | bidirectional |
v.push_front(X)
; v.insert(v.begin(),
X)
is the equivalent.
operator []
; std::advance(l.begin(),
i)
is the equivalent.
std::find()
is a generic algorithm not re-implemented by either
vectors or lists.
vector | list | |
---|---|---|
c.insert(i, v) |
j >= i , possibly all |
none |
c.erase(i) |
j >= i |
i |
c.push_back(v) |
c.end() , possibly all |
none |
c.pop_back() |
--c.end() , c.end() |
--c.end() |
c.push_front(v) |
all1 | c.begin() |
c.pop_front() |
all2 | c.begin() |
v.push_front(X)
; v.insert(v.begin(),
X)
is the equivalent.
pop_front()
; v.erase(v.begin())
is the equivalent.
.insert_after()
member functions are O(1) iterators.
.splice()
, .remove()
, .unique()
, .merge()
, and
.sort()
.
This page last modified on 24 February 2004.