List
Lists are a kind of sequence containers. As such, their elements are ordered following a linear sequence.
List
containers are implemented as doubly-linked lists; Doubly linked lists
can store each of the elements they contain in different and unrelated
storage locations. The ordering is kept by the association to each
element of a link to the element preceding it and a link to the element
following it.
This provides the following advantages to list containers:
- Efficient insertion and removal of elements anywhere in the container (constant time).
- Efficient moving elements and block of elements within the container or even between different containers (constant time).
- Iterating over the elements in forward or reverse order (linear time).
Compared to other base standard sequence containers (s and s),
lists perform generally better in inserting, extracting and moving
elements in any position within the container, and therefore also in
algorithms that make intensive use of these, like sorting algorithms.
The main drawback of s
compared to these other sequence containers is that they lack direct
access to the elements by their position; For example, to access the
sixth element in a
one has to iterate from a known position (like the beginning or the
end) to that position, which takes linear time in the distance between
these. They also consume some extra memory to keep the linking
information associated to each element (which may be an important
factor for large lists of small-sized elements).
Storage is handled automatically by the class, allowing lists to be expanded and contracted as needed.
In their implementation in the C++ Standard Template Library lists take two template parameters:
template < class T, class Allocator = allocator > class list; |
Where the template parameters have the following meanings:
- T: Type of the elements.
- Allocator: Type of the allocator object used to define the storage allocation model. By default, the allocator class template for type T is used, which defines the simplest memory allocation model and is value-independent.
In the reference for the
list member functions, these same names are assumed for the template parameters.
Member functions
| Construct list (public member function) |
| List destructor (public member function) |
| Copy container content (public member function) |
Iterators:
| Return iterator to beginning (public member function) |
| Return iterator to end (public member function) |
| Return reverse iterator to reverse beginning (public member function) |
| Return reverse iterator to reverse end (public member function) |
Capacity:
| Test whether container is empty (public member function) |
| Return size (public member function) |
| Return maximum size (public member function) |
| Change size (public member function) |
Element access:
| Access first element (public member function) |
| Access last element (public member function) |
Modifiers:
| Assign new content to container (public member function) |
| Insert element at beginning (public member function) |
| Delete first element (public member function) |
| Add element at the end (public member function) |
| Delete last element (public member function) |
| Insert elements (public member function) |
| Erase elements (public member function) |
| Swap content (public member function) |
| Clear content (public member function) |
Operations:
| Move elements from list to list (public member function) |
| Remove elements with specific value (public member function) |
| Remove elements fulfilling condition (public member function template) |
| Remove duplicate values (member function) |
| Merge sorted lists (public member function) |
| Sort elements in container (public member function) |
| Reverse the order of elements (public member function) |
Allocator:
| Get allocator (public member function) |
Member types
of
template > class list;
member type | definition |
reference | Allocator::reference |
const_reference | Allocator::const_reference |
iterator | Bidirectional iterator |
const_iterator | Constant bidirectional iterator |
size_type | Unsigned integral type (usually same as ) |
difference_type | Signed integral type (usually same as ) |
value_type | T |
allocator_type | Allocator |
pointer | Allocator::pointer |
const_pointer | Allocator::const_pointer |
reverse_iterator | reverse_iterator |
const_reverse_iterator | reverse_iterator |
阅读(1050) | 评论(0) | 转发(0) |