The list
container class represents a list of elements that can be accessed sequentially. Unlike vector
and deque
, list
does not support random access. Insertions and deletions from a list
can be performed in constant time, and all of the properties of linked lists you have learned from CS106B/X apply to the list
container.
list
intrinsically supports a huge number of operations normally only accessible through algorithms, such as sort
, merge
, and splice
, which work very quickly over a list
.
Constructs a new list. This is the default constructor.
Constructs a new list with the specified number of copies of the specified element. With a single parameter, this constructor creates a list containing the specified number of elements, all initialized to the default. With two parameters, this constructor creates a list of the specified number of copies of the specified element.
Returns the number of elements stored in the container.
Returns whether the list is empty. It's considered good practice to call empty
to determine whether the list is empty rather than checking whether size()
returns zero.
Removes all elements from the list
.
Resizes the list by adding or removing elements from the end of the list. The first version resizes the list to the specified size, trimming elements from the end if the new size is smaller than the old size and padding the end with new objects if the new size is greater than the old size. The newly-added elements are constructed using the default constructor.
The second version of the function works identically to the first, except that any newly-added elements are initialized to have the same value as the parameter.
Returns an iterator to the first element in the list.
Returns an iterator to the element one past the end of the list. This iterator does not point to a valid element, so do not dereference it.
Appends the specified element to the back of the list, increasing the size by one.
Removes the last element in the list. pop_back
does not return a value, so if you want to get the value, call back
first. pop_back
does not do any bounds-checking, so make sure the list is nonempty before calling.
Returns a reference to the last element of the list. There is no bounds-checking, so make sure the list is nonempty before calling.
Prepends the specified element to the front of the list, increasing the size by one.
Removes the first element in the list. pop_front
does not return a value, so if you want to get the value, call front
first. pop_front
does not do any bounds-checking, so make sure the list is nonempty before calling.
Returns a reference to the first element of the list. There is no bounds-checking, so make sure the list is nonempty before calling.
The first version inserts the specified element at the position indicated by the specified iterator. There is no bounds-checking. The size of the container will grow by one and elements will be shifted down.
The second version inserts the specified number of copies of the specified element at the specified position.
The third version inserts the elements in the specified iterator range into the list at the specified position. The source iterators can be of any type.
Removes elements from the list without bounds-checking. The first version removes the element pointed at by the iterator, and the second removes all elements in the specified range.
Assuming that the list
is sorted, merges the list and the other sorted list
other
so that the calling list
now contains the elements of both lists in sorted order. The parameter other
becomes empty. If the two list
s are sorted according to a special comparison function, you can specify it as the second parameter to the function.
This function is best illustrated by example. Suppose you have two list<int>
s, list1
and list2
. The contents of list1
are
{0, 2, 4, 6, 8, 10
}
And the contents of list2
are
{1, 3, 5, 7, 9, 11
}
If you call list1.merge(list2)
, then list2
will become empty and list1
will contain
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}
In that order.
Removes all elements from the list whose value is equal to that of elem
. To remove elements by iterator rather than value, use erase
.
Removes all elements from the list
for which the predicate function pred
returns true.
Reverses the elements of the list. Iterators over the list
are unaffected.
Sorts the list according to the <
operator. To use a special comparison function instead, pass it as a parameter.
list
into this oneMoves elements from one list
into the calling object. The elements are removed from the other list
and inserted into the caller at the index specified by the pos
iterator.
The first version of this function empties the first list
and inserts all its elements into the caller at the specified position. Note that the list
specified as a parameter will become empty.
The second version of the function moves only the element specified by the elem
parameter into the caller list. The rest of the other list
will be unmodified.
The third version of the function moves all elements from the specified iterator range into the caller list
.
For each group of adjacent identical elements in the list
, unique
removes all but one of them. This function should most likely be called on a sorted list
, since otherwise the resulting values of the list
are not guaranteed to be unique.
Normally, elements are compared using the ==
operator. To override this behavior, you can pass a comparison function to unique
as a parameter. This function should return true
if the two elements are equal and false
otherwise.
Returns a reverse_iterator
to the last element of the list
. reverse_iterator
s are like regular iterators except that they traverse in the opposite direction. Check for the end-of-container condition of reverse_iterator
s with rend
.
Returns a reverse_iterator
to the element one element before the start of the array. Like end
, the iterator returned by rend
does not actually reference a real element of the list
and you should take care not to actually dereference it.
Returns the maximum number of elements the list
can hold, which is usually the number of unique memory addresses divided by the size of the elements stored in the container.
list
sExchanges the contents of this list
and another list
.
list
content.Erases any stored content and replaces it with either the specified number of repeats of the specified elements or the elements contained in the range [start, end). In either case, the original list
contents are deleted.