Overview
The standard library provides list container.
Details
list is basically template based doubly linked list.
Syntax
The syntax is as below. Template parameter T represents the datatype to store and Alloc represents the allocator used for storage.
template < class T, class Alloc = allocator<T> >class list;
Members
It defines following types
Name | Description |
---|---|
value_type | The first template parameter (T) |
allocator_type | The second template parameter (Alloc) |
reference | value_type& |
const_reference | const value_type& |
pointer | allocator_traits<allocator_type>::pointer |
const_pointer | allocator_traits<allocator_type>::const_pointer |
iterator | a bidirectional iterator to value_type convertible to const_iterator |
const_iterator | a bidirectional iterator to const value_type |
reverse_iterator | reverse_iterator<iterator> |
const_reverse_iterator | reverse_iterator<const_iterator> |
difference_type | a signed integral type |
size_type | an unsigned integral type |
Operation
list can be graphically represented as below. Each node has links to the previous and next nodes.
New elements can be added or removed in the end or in the middle of the list. Each node has links to the previous and next nodes.
Complexity
Random access operation is not supported. Insertion or removal at the front or end is O(1). Insertion or removal at the middle is O(n) .
Functionality
Constructors
In following constructors use default allocator. Custom allocators can be used by passing them as an additional argument.
Name | Description |
---|---|
list() | Default Constructor. Example: //v:{} list<int> v(); |
list(size_type n) | Constructs a container with n elements initialized with T(). Example: //v:{0,0,0,0,0} list<int> v(5); |
list(size_type n, const value_type& val) | Constructs a container with n elements initialized with with val. Example: //v:{10,10,10,10,10}
list<int> v(5,10); |
list(InputIterator first, InputIterator last) | Constructs a container and copies the elements in the range. Example: int a[5]{1,2,3,4,5};
|
list(const list& x) | copy constructor. |
list(list&& x) | move constructor. |
list(initializer_list<value_type> il) | initializer_list constructor. Example: //v:{1,2,3,4,5} list<int> v{1,2,3,4,5}; |
Iterator
Name | Description |
---|---|
iterator begin() iterator end() | Returns iterator to beginning and end of the list. Example: list<int> v{1,2,3,4,5};//prints 1 2 3 4 5for (auto itr=v.begin(); itr!=v.end(); ++itr) cout << *itr << ' '; |
reverse_iterator rbegin() reverse_iterator rend() | Returns reverse_iterator to reverse beginning and reverse end of the list. Example: list<int> v{1,2,3,4,5};//prints 5 4 3 2 1for (auto itr=v.rbegin(); itr!=v.rend(); ++itr) cout << *itr << ' '; |
const_iterator cbegin() const_iterator cend() | Returns const_iterator to beginning and end of the list. Example: list<int> v{1,2,3,4,5};//prints 1 2 3 4 5for (auto itr=v.cbegin(); itr!=v.cend(); ++itr) cout << *itr << ' '; |
const_reverse_iterator crbegin() const_reverse_iterator crend() | Return const_reverse_iterator to reverse beginning and reverse end of the list. Example: list<int> v{1,2,3,4,5};//prints 5 4 3 2 1for (auto itr=v.crbegin(); itr!=v.crend(); ++itr) cout << *itr << ' '; |
Capacity
Name | Description |
---|---|
size_type size() | Returns the number of elements. Example: list<int> v{1,2,3,4,5};//prints 5cout << v.size(); |
size_type max_size() | Returns maximum possible number of elements possible. A very large number. |
bool empty() | Test whether container is empty. |
Element Access
Name | Description |
---|---|
reference front() | Returns reference or const reference to the first element. Exception is thrown for invalid input.Example:list<int> v{1,2,3,4,5}; |
reference back() | Returns reference or const reference to the last element. Exception is thrown for invalid input.Example:list<int> v{1,2,3,4,5}; |
Modifiers
Name | Description |
---|---|
|
|
Example:int a[]{1,2,3,4,5}; list<int> v{};v.assign(begin(a)+2,end(a)); | |
void push_back(const value_type val) | Adds val at the end of the list.Example:list<int> v{1};v.push_back(2); |
void pop_back() | Removes the last element from the list.Example:list<int> v{1,2}; //v:{1} v.pop_back(); |
void push_front(const value_type val) | Adds element at the front.Example:list<int> v{2};v.push_front(1); |
void pop_front() | Deletes first element.Example:list<int> v{1,2}; //v:{2} v.pop_back(); |
|
|
Example:
int a[]{1,2,3}; list<int> v{}; //(1) v:{1,2,3} //it=begin(v)+0 auto it = v.insert(v.begin(),begin(a),end(a)); //(2) v:{1,2,3,4} //it=begin(v)+3 it = v.insert(v.end(),1,4); //(3) v:{1,2,3,4,5} //it=begin(v)+4 it = v.insert(v.end(),5); //(4) v:{1,2,3,4,5,6} //it=begin(v)+5 it = v.insert(v.end(),move(6)); //(5) v:{1,2,3,4,5,6,7} //it=begin(v)+6 it = v.insert(v.end(),{7}); | |
|
|
Example: list<int> v{1,2,3,4,5}; //(1) // v:{2,3,4,5} //it = begin(v) auto it = v.erase(cbegin(v)); //(2) // v:{2,3} //it = begin(v)+2 ++it;++it; it = v.erase(it,cend(v)); | |
void swap(vector& v) | Swap content with v. Note T has to be same. Example: list<int> v{1,2,3,4,5}; list<int> v2{5,4,3,2,1};
|
void clear() | Clear contents. Example: list<int> v{1,2,3,4,5};
|
iterator emplace((const_iterator pos, Args ...arg) | Construct and insert element in place using arg. Returns an iterator to the newly inserted element. Example: list<int> v{1,2,3,5};
|
void emplace_back(Args ...arg)) | Construct and insert element at the end using arg. Example: list<int> v{1,2,3,4};
|
void emplace_front(Args ...arg)) | Construct and insert element at the beginning using arg. Example: list<int> v{2,3,4};
|
|
|
Example:list<int> v{1,2,3,4,5}; | |
|
|
Example: list<int>::const_iterator operator+ (list<int>::const_iterator it, list<int>::size_type i) { advance(it,i); return it; }list<int> v; list<int> v2; list<int>::iterator it;it = begin(v); | |
void remove_if( UnaryPredicate p ); | Removes the elements from the list that returns true for the p. Example: list<int> v{1,2,3,4,5,2};
|
|
|
Example:list<int> v{1,2,2,3,4,5,2}; | |
void remove(const value_type& val) | Removes the elements of value val from the list. Example: list<int> v{1,2,3,4,5,2};
|
|
|
Example:list<int> v; list<int> v2; | |
|
|
Example:list<int> v; | |
void reverse() | Reverses the elements in the list. Example: list<int> v{1,3,2,5,4,5,6,4};
|
No comments:
Post a Comment