Select Page

# list

A list is implemented as a doubly linked list data structure and is thus designed for rapid insertion and removal of elements anywhere in the sequence, whereas for vector and deque this is a much more costly operation. A list is so slow when randomly accessing elements that it does not have an operator[ ]. It?s best used when you?re traversing a sequence, in order, from beginning to end (or vice-versa), rather than choosing elements randomly from the middle. Even then the traversal is significantly slower than either a vector or a deque, but if you aren?t doing a lot of traversals, that won?t be your bottleneck.
Another thing to be aware of with a list is the memory overhead of each link, which requires a forward and backward pointer on top of the storage for the actual object. Thus, a list is a better choice when you have larger objects that you?ll be inserting and removing from the middle of the list. It?s better not to use a list if you think you might be traversing it a lot, looking for objects, since the amount of time it takes to get from the beginning of the list?which is the only place you can start unless you?ve already got an iterator to somewhere you know is closer to your destination?to the object of interest is proportional to the number of objects between the beginning and that object.

The objects in a list never move after they are created; ?moving? a list element means changing the links, but never copying or assigning the actual objects. This means that an existing iterator never moves when you add new things to a list as it was demonstrated to do in vector. Here?s an example using the Noisy class:

Operations as seemingly radical as reversing and sorting the list require no copying of objects, because instead of moving the objects, the links are simply changed. However, notice that sort( ) and reverse( ) are member functions of list, so they have special knowledge of the internals of list and can move the pointer instead of copying it. On the other hand, the swap( ) function is a generic algorithm and doesn?t know about list in particular, so it uses the copying approach for swapping two elements. In general you should use the member version of an algorithm if it is supplied instead of its generic algorithm equivalent. In particular, use the generic sort( ) and reverse( ) algorithms only with arrays, vectors, and deques.

If you have large, complex objects, you might want to choose a list first, especially if construction, destruction, copy-construction, and assignment are expensive and if you are doing things like sorting the objects or otherwise reordering them a lot.

Special list operations
The list has some special built-in operations to make the best use of the structure of the list. You?ve already seen reverse( ) and sort( ), and here are some of the others in use:

The print( ) function displays results. After filling four lists with Noisy objects, one list is spliced into another in three ways. In the first, the entire list l2 is spliced into l1 at the iterator it1. Notice that after the splice, l2 is empty?splicing means removing the elements from the source list. The second splice inserts elements from l3 starting at it2 into l1 starting at it1. The third splice starts at it1 and uses elements from l4 starting at it3 and ending at it4 (the seemingly redundant mention of the source list is because the elements must be erased from the source list as part of the transfer to the destination list).
The output from the code that demonstrates remove( ) shows that the list does not have to be sorted in order for all the elements of a particular value to be removed.

Finally, if you merge( ) one list with another, the merge only works sensibly if the lists have been sorted. What you end up with in that case is a sorted list containing all the elements from both lists (the source list is erased?that is, the elements are moved to the destination list).
A unique( ) member function removes all duplicates, but only if the list has been sorted first:

The list constructor used here takes the starting and past-the-end iterator from another container and copies all the elements from that container into itself. (A similar constructor is available for all the containers.) Here, the ?container? is just an array, and the ?iterators? are pointers into that array, but because of the design of the STL, it works with arrays just as easily as any other container.

The unique( ) function will remove only adjacent duplicate elements, and thus sorting is necessary before calling unique( ).

Four additional list member functions are not demonstrated here: a remove_if( ) that takes a predicate, which is used to decide whether an object should be removed; a unique( ) that takes a binary predicate to perform uniqueness comparisons; a merge( ) that takes an additional argument which performs comparisons; and a sort( ) that takes a comparator (to provide a comparison or override the existing one).

list vs. set
Looking at the previous example, you might note that if you want a sorted list with no duplicates, a set can give you that, right? It?s interesting to compare the performance of the two containers:

When you run the program, you should discover that set is much faster than list. This is reassuring?after all, it is set?s primary job description!

1 Shares
Share1
+1
Tweet
Share
Stumble