Friday, January 1, 2010

STL LIST C++

List

header:

Lists are a kind of sequence containers. As such, their elements are ordered following a linear sequence.

List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept by the association to each element of a link to the element preceding it and a link to the element following it.

This provides the following advantages to list containers:
1.Efficient insertion and removal of elements anywhere in the container (constant time).
2.Efficient moving elements and block of elements within the container or even between different containers (constant time).
3.Iterating over the elements in forward or reverse order (linear time).


Compared to other base standard sequence containers (vectors and deques), lists perform generally better in inserting, extracting and moving elements in any position within the container, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in a list one has to iterate from a known position (like the beginning or the end) to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements).

eg.

list Obj; //declared an int list
list::iterator itr= Obj.begin(); //declared an iterator to int list

No comments:

Post a Comment