CMPU-102: Computer Science II

C++ Standard Library List Class

This page provides a brief overview of the C++ Standard Library list template class and its iterators and operations. The STL library was created with the primary goal of being as efficient as possible, and with the secondary goal of being as general as possible. For the list class, the following methods are defined (T is the type used to create the template, i.e. list, and I is an iterator):

Typename/argsDescription
Iteratorsbegin()Points to the first element
end()Points past (after) the last element
rbegin()Points to the last element (the first element of the reverse sequence)
rend()Points past (before) the first element (the end of a reverse sequence)
List Opspush_back(T)Append an element to the end.
pop_back()Removes (does not return) the last element.
push_front(T)Insert a new first element
pop_front()Removes (does not return) the first element.
insert(I,T)Insert an element before I.
insert(n,I,T)Insert n copies of the element before I.
insert(I, I1, I2)Insert elements from a sequence bounded by I1 (inclusive) and I2 (exclusive) before I (may be the same or a different sequence than I points to, but must be of the same type).
erase(I)Delete/remove element.
erase(I1, I2)Delete/remove elements between I1 and I2.
clear()Delete/remove all elements. Note:Not implemented in g++, use: erase(l.begin(),l.end())
Other Opssize()Number of elements.
empty()Is the list empty?
==Are the contents of two containers the same?
=Copy one container into another.
Algorithmsfind(I1,I2,T)Find an element (first occurence) in a sequence, returns an iterator.
count(I1,I2,T)Count the number of times the element occurs in the sequence.
search(I1,I2,I3,I4)Find the first occurrence of the sequence [I3,I4) within the sequence [I1,I2). Returns an iterator.