C++ list Library
C++ list Functions
The <list> library has functions that let you work with lists. A list stores elements in order, like a vector, and you can add and remove elements at both the front and the back.
Unlike vectors, lists do not have index numbers, so you cannot use [] or at(). Instead, lists have their own functions to sort, merge and reverse their elements.
A list of popular list functions can be found in the table below.
| Function | Description |
|---|---|
| back() | Returns the last element of a list |
| begin() | Returns an iterator pointing to the first element of a list |
| clear() | Removes all the elements from a list |
| empty() | Checks whether a list is empty or not |
| end() | Returns an iterator pointing to the position after the last element of a list |
| erase() | Removes one element, or a range of elements, from a list |
| front() | Returns the first element of a list |
| insert() | Inserts one or more elements at a specified position in a list |
| merge() | Moves all elements from another sorted list into a sorted list, so the result is also sorted |
| pop_back() | Removes the last element of a list |
| pop_front() | Removes the first element of a list |
| push_back() | Adds an element to the end of a list |
| push_front() | Adds an element to the beginning of a list |
| remove() | Removes all the elements that have a specified value |
| reverse() | Reverses the order of the elements in a list |
| size() | Returns the number of elements in a list |
| sort() | Sorts the elements of a list |
| unique() | Removes duplicate elements that are next to each other in a list |