list |
A list
is a sequential container that is optimized for insertion and erasure at
arbitrary points at the expense of giving up indexed based access. This section
provides an overview of list .
A list
has a bidirectional iterator.
A list
is normally implemented as a doubly-linked list in which every node in the list
has a pointer to the previous node and a pointer to the next node.
All list
objects of a particular type share a common pool of free nodes. This reduces the
amount of unnecessary heap access. When a node is no longer used by a list
, it is placed back into the pool so it can be reused. When the number of list
objects of a particular type drops to zero, the nodes in the associated common
pool are deallocated.
For compilers that do not support templatized destruction, this pool approach cannot be used and a simpler but less efficient memory allocation strategy is used.
Inserting a single item into a list
takes constant time with one call to the copy constructor.
Inserting multiple items into a list
is linear in the number of items inserted. The number of calls to the copy
constructor is equal to the number of items inserted.
Insertion does not affect iterators or references.
Erasing a single item is a
constant time operation with a single call to the destructor. Erasing a range in
a list is linear time in the size of the range, and
the number of calls to the destructor is equal to the size of the range. An
erasure invalidates only the iterators and references to the erased items.
A list
is useful when the order of items and fast arbitrary insertion and erasure are
important. A list is not as efficient as a deque
when insertion and erasure only take place at the extremities.
A list
has a more elaborate interface than any of the other sequential containers. It
includes functions for splicing, removing, filtering, merging, and sorting. This
section contains examples of each of these features.
The
splicing family of functions moves a group of one or more items from one list
to another.
void
splice( iterator pos ,
list< T, Allocator >& list )&list != this .void
splice( iterator to ,
list< T, Allocator >& list ,
iterator from )splice() does not require that &list
!= this .void
splice( iterator pos ,
list< T, Allocator >& list
, iterator first
, iterator last
)splice() does not require that &list
!= this . The following example shows use of
the third variation of splice()
.
#include <iostream>
#include <list>
int array1[] = { 1, 16 };
int array2[] = { 4, 9 };
void
main()
{
list< int > l1( array1, array1 + 2 );
list< int > l2( array2, array2 + 2 );
list< int >::iterator i = l1.begin();
++i;
l1.splice( i, l2, l2.begin(), l2.end() );
i = l1.begin();
while ( i != l1.end() )
cout << *i++ << "\n";
}
1
4
9
16
The list
container also contains several useful functions that perform the same tasks
as the STL algorithms with the same name, except they are optimized for the
internal architecture of a list .
void
remove( const T& value )==
to perform the comparison.void
reverse()list . The time complexity is
O(N).void
unique()void
sort()list
elements using operator< . The time
complexity is O(NlogN). Note that unlike the algorithm equivalent of this
operation, there is no variation of this function that takes a comparison
operator.Following is an example demonstrating each of these four functions.
#include <iostream>
#include <list>
char array[] = { `x', `l', `x', `t', `s', `s' };
void
main()
{
list< char > str( array, array + 6 );
list< char >::iterator i;
cout << "original: ";
for ( i = str.begin(); i != str.end(); ++i )
cout << *i;
cout << "\n";
str.reverse();
cout << "reversed: ";
for ( i = str.begin(); i != str.end(); ++i )
cout << *i;
cout << "\n";
str.remove( `x' );
cout << "removed: ";
for ( i = str.begin(); i != str.end(); ++i )
cout << *i;
cout << "\n";
str.unique();
cout << "uniqued: ";
for ( i = str.begin(); i != str.end(); ++i )
cout << *i;
cout << "\n";
str.sort();
cout << "sorted: ";
for ( i = str.begin(); i != str.end(); ++i )
cout << *i;
cout << "\n";
}
original: xlxtss
reversed: sstxlx
removed: sstl
uniqued: stl
sorted: lst
The list
container supplies a specialized version of the merge()
algorithm optimized
for merging lists.
void
merge( const list< T, Allocator >& source
)list and sorts the result using
the operator<. This operation assumes that
both list and source are
already sorted using operator<.
#include <iostream>
#include <list>
int array1[] = { 1, 3, 6, 7 };
int array2[] = { 2, 4 };
void
main()
{
list< int > l1( array1, array1 + 4 );
list< int > l2( array2, array2 + 2 );
l1.merge( l2 );
for ( int_list::iterator i = l1.begin(); i != l1.end(); ++i )
cout << *i << " ";
cout << "\n";
}
1 2 3 4 6 7
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.