![]() |
VOOZH | about |
A list in C++ is a sequence container that allows you to store elements one after another.
List elements: 5 10 20
A list is defined as the std::list class template inside the <list> header file.
list<T> l;
where,
The basic operations of list are shown below:
1 3 4 2 5
1 5 4
Explanation: "next(l.begin(), 2)" moves the iterator two positions forward from the start of the list and * dereferences it to print the element at index 2 (the third element).
11 3 10 2 5
4
1 3 4 2 5
3 4
Following is the list of all member functions of std::list class in C++:
Functions | Description |
|---|---|
| Returns the number of elements in the list. | |
| Checks if the list is empty. | |
| Returns a reverse iterator pointing to the last element of the list. | |
Returns a reverse iterator pointing to the element before the first element. | |
| Removes all elements from the list. |
Feature | forward_list | list |
|---|---|---|
Type of linked List | Singly linked list | Doubly linked list |
Traversal | Can only traverse forward | Can traverse both forward and backward |
Memory usage | Uses less memory (only one pointer per node) | Uses more memory (two pointers per node) |
Insertion/Deletion | Fast insertion and deletion, but only at or after a given position | Fast insertion and deletion anywhere (before or after a position) |
Functions supported | Limited compared to list (no size(), no reverse iterators) | More complete interface including size(), reverse(), bidirectional iterators. |