List Flashcards
Element access
front( )
access the first element
Parameters
(none)
Return value
reference to the first element
Complexity
Constant
Element access]
back( )
access the last element
Parameters
(none)
Return value
Reference to the last element.
Complexity
Constant.
Iterators
begin
cbegin( )
returns an iterator to the beginning
Parameters
(none)
Return value
Iterator to the first element.
Complexity
Constant.
Iterators
end
cend( )
returns an iterator to the end
Parameters
(none)
Return value
Iterator to the element following the last element.
Complexity
Constant.
Iterators
rbegin
crbegin( )
returns a reverse iterator to the beginning
Parameters
(none)
Return value
Reverse iterator to the first element.
Complexity
Constant.
Iterators
rend
crend( )
returns a reverse iterator to the end
Parameters
(none)
Return value
Reverse iterator to the element following the last element.
Complexity
Constant.
Capacity
empty( )
checks whether the container is empty
Parameters
(none)
Return value
true if the container is empty, false otherwise
Complexity
Constant.
Capacity
size( )
returns the number of elements
Parameters
(none)
Return value
The number of elements in the container.
Complexity
Constant or linear. (until C++11)
Constant. (since C++11)
Capacity
max_size( )
returns the maximum possible number of elements
Parameters
(none)
Return value
Maximum number of elements.
Complexity
Constant.
Modifiers
clear( )
clears the contents
Parameters
(none)
Return value (none)
Complexity
Linear in the size of the container, i.e., the number of elements.
Modifiers
insert( )
inserts elements
Parameters
pos - iterator before which the content will be inserted. pos may be the end() iterator
value - element value to insert
first, last - the range of elements to insert, can’t be iterators into container for which insert is called
ilist - initializer list to insert the values from
Type requirements
-T must meet the requirements of CopyInsertable in order to use overload (1).
-T must meet the requirements of MoveInsertable in order to use overload (2).
-T must meet the requirements of CopyAssignable and CopyInsertable in order to use overload (3).
-T must meet the requirements of EmplaceConstructible in order to use overload (4,5).
Return value
1-2) Iterator pointing to the inserted value
3) Iterator pointing to the first element inserted, or pos if count==0.
4) Iterator pointing to the first element inserted, or pos if first==last.
5) Iterator pointing to the first element inserted, or pos if ilist is empty.
Complexity
1-2) Constant.
3) Linear in count
4) Linear in std::distance(first, last)
5) Linear in ilist.size()
Modifiers
emplace( )
constructs element in-place
Parameters
pos - iterator before which the new element will be constructed
args - arguments to forward to the constructor of the element
Type requirements
-T (the container’s element type) must meet the requirements of EmplaceConstructible.
Return value
Iterator pointing to the emplaced element.
Complexity
Constant.
Modifiers
erase( )
erases elements
Parameters
pos - iterator to the element to remove
first, last - range of elements to remove
Return value
Iterator following the last removed element.
If pos refers to the last element, then the end() iterator is returned.
If last==end() prior to removal, then the updated end() iterator is returned.
If [first, last) is an empty range, then last is returned.
Exceptions
(none)
Complexity
1) Constant.
2) Linear in the distance between first and last.
Modifiers
push_back( )
adds an element to the end
Parameters
value - the value of the element to append
Type requirements
-T must meet the requirements of CopyInsertable in order to use overload (1).
-T must meet the requirements of MoveInsertable in order to use overload (2).
Return value
(none)
Complexity
Constant.
Modifiers
emplace_back( )
constructs an element in-place at the end
Parameters
args - arguments to forward to the constructor of the element
Type requirements
-T (the container’s element type) must meet the requirements of EmplaceConstructible.
Return value
(none)
(until C++17)
A reference to the inserted element.
(since C++17)
Complexity
Constant.