English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
List is a continuous container, while vector is a non-continuous container, that is, list stores elements in continuous memory, while vector stores in non-continuous memory.
Insertion and deletion in the middle of the vector (vector) are very expensive because it takes a lot of time to move all the elements. The list overcomes this problem and is implemented using the list container.
List supports bidirectional and provides an effective method for insertion and deletion operations.
The traversal speed in the list is very slow because the list elements are accessed in order, while vector supports random access.
#include<iostream> #include<list> using namespace std; int main() { list<int> l; }
It creates an empty list of integer type values.
The list can also be initialized with parameters.
#include<iostream> #include<list> using namespace std; int main() { list<int> l{1,2,3,4}; }
The list can be initialized in two ways.
list<int> new_list{1,2,3,4}; or list<int> new_list = {1,2,3,4};
The following are the member functions of the list:
Method | Description |
---|---|
insert() | It inserts a new element before the position pointed to by the iterator. |
push_back() | It adds a new element at the end of the container. |
push_front() | It adds a new element at the front. |
pop_back() | Delete the last element. |
pop_front() | Delete the first element. |
empty() | It checks if the list is empty. |
size() | It finds the number of existing elements in the list. |
max_size() | It finds the maximum size of the list. |
front() | It returns the first element of the list. |
back() | It returns the last element of the list. |
swap() | It swaps two lists when their types are the same. |
reverse() | It reverses the elements of the list. |
sort() | It sorts the elements in the list in ascending order. |
merge() | It merges two sorted lists. |
splice() | It will insert a new list into the calling list. |
unique() | It removes all duplicate elements from the list. |
resize() | It changes the size of the list container. |
assign() | It will allocate a new element to the list container. |
emplace() | It will insert a new element at the specified position. |
emplace_back() | It will insert a new element at the end of the container. |
emplace_front() | It will insert a new element at the beginning of the list. |