Sequence Containers:
List:
—-double-linked lists
List are sequence containers that allow constant time insert and eraser operations anywhere within the sequence, and iteration in both directions.
Implemented as: Doubly-linked lists
,so doesn’t support [ ] (direct access )
They are very similar to forward_list:
The main difference being that forward_list objects are single-linked lists,
and thus they can only be iterated forwards, in exchange for being somewhat smaller and more efficient.
Compared to array and vector and deque, lists generally better inserting,extracting and moving elements in any position
. Algorithms that make intensive use of these, like sorting algorithms.
Function:
1. operator=
Assigns new contents to the container, replacing its current contents, and modifying its size accordingly.
Iterators:
begin ,end, rbegin,rend,cbegin,cend,crbegin,crend;Capacity:
empty,size,max_size(return the max num of elements that the list container can holder)
eg:if (i<mylist.max_size()) mylist.resize(i);
Element access:
front,backModifiers:
Assign( ) like operator “=”
emplace_front( args ) : Inserts a new element at the beginning of the list, right before its current first element. This new element is constructedin place
using args as the arguments for its construction.
A similar member function exists, push_front, which either copies or moves an existing object into the container.
emplace_back(args)
push_front( ); push_back ( );
pop_front( ); pop_back( );
Insert( )
: inserting new elements before the element at the specified position.
eg:
mylist.insert (it,10);
mylist.insert (it,2,20);
mylist.insert (it,myvector.begin(),myvector.end());
erase(args ):
iterator erase (iterator position);—-single element
iterator erase (iterator first, iterator last);—-range of elements
PS: eraser( ) is wrong
Swap( list &x ):Exchanges the content of the container by the content of x.
std::list<int>().swap(first); to clear all first elements
Resize( ):
Resizes the container so that it contains n elements. If n is smaller
than the current container size, the content is reduced to its first n elements
, removing those beyond (and destroying them).
clear( ):
Removes all elements from the list container (which are destroyed), and leaving the container with a size of 0.
Advance (iterator,n)
Advances the iterator it by n element positions.
If it is a random-access iterator
, the function uses just once operator+ or operator-. Otherwise, the function uses repeatedly the increase or decrease operator
(operator++ or operator–) until n elements have been advanced.
Splice( )
Splice(iterator position,list &x)
Splice(iterator position,list &x,iterator i)
Splice(iterator position,list &x,iterator first, ,iterator last)
Transfers elements from x into the container, inserting them at position.
remove( const value_type& val);
Removes from the container all the elements that compare equal to val
compared with member function list::erase, which erases elements by their position
(using an iterator), this function (list::remove) removes elements by their value
.
remove_if( )
Remove elements fulfilling condition
unique( )
Remove duplicate values
merge( )
Merge sorted lists
sort( )—-stable,approximately NlogN where N is the container size.Sorts the elements in the list, altering their position within the container.
Reverse( )
Reverse the order of elements