Top of document
©Copyright 1999 Rogue Wave Software

list


     Container

Summary

A sequence that supports bidirectional iterators

Contents

Synopsis

#include <list>
template <class T, class Allocator = allocator>
class list;

Description

list<T,Allocator> is a type of sequence that supports bidirectional iterators. A list<T,Allocator> allows constant time insert and erase operations anywhere within the sequence, with storage management handled automatically. Constant time random access is not supported.

Any type used for the template parameter T must provide the following (where T is the type, t is a value of T and u is a const value of T):

  Default constructor   T()
  Copy constructors     T(t) and T(u)
  Destructor            t.~T()
  Address of            &t and &u yielding T* and
                         const T* respectively
  Assignment            t = a where a is a
                         (possibly const) value of T

Interface

template <class T, class Allocator = allocator>
 class list {
public:
// typedefs
   class iterator;
   class const_iterator;
   typename reference;
   typename const_reference;
   typename size_type;
   typename difference_type;
   typedef T value_type;
   typedef Allocator allocator_type;
   typename reverse_iterator;
   typename const_reverse_iterator;
// Construct/Copy/Destroy
   explicit list (const Allocator& = Allocator());
   explicit list (size_type, const Allocator& = Allocator());
   list (size_type, const T&, const Allocator& = Allocator())
   template <class InputIterator>
   list (InputIterator, InputIterator, 
         const Allocator& = Allocator());
   list(const list<T, Allocator>& x);
   ~list();
   list<T,Allocator>& operator= (const list<T,Allocator>&);
   template <class InputIterator>
    void assign (InputIterator, InputIterator);
   template <class Size, class T>
    void assign (Size n);
   template <class Size, class T>
    void assign (Size n, const T&);
   allocator_type get allocator () const;
// Iterators
   iterator begin ();
   const_iterator begin () const;
   iterator end ();
   const_iterator end () const;
   reverse_iterator rbegin ();
   const_reverse_iterator rbegin () const;
   reverse_iterator rend ();
   const_reverse_iterator rend () const;
// Capacity
   bool empty () const;
   size_type size () const;
   size_type max_size () const;
   void resize (size_type);
   void resize  (size_type, T);
// Element Access
   reference front ();
   const_reference front () const;
   reference back ();
   const_reference back () const;
// Modifiers
   void push_front (const T&);
   void pop_front ();
   void push_back (const T&);
   void pop_back ();
   iterator insert (iterator);
   iterator insert (iterator, const T&);
   void insert (iterator, size_type, const T&);
   template <class InputIterator>
    void insert  (iterator, InputIterator, InputIterator);
   iterator erase (iterator);
   iterator erase (iterator, iterator);
   void swap (list<T, Allocator>&);
   void clear ();
// Special mutative operations on list
   void splice (iterator, list<T, Allocator>&);
   void splice (iterator, list<T, Allocator>&, iterator);
   void splice (iterator, list<T, Allocator>&, iterator, iterator);
   void remove (const T&);
   template <class Predicate>
    void remove_if (Predicate);
   void unique ();
   template <class BinaryPredicate>
    void unique (BinaryPredicate);
   void merge (list<T, Allocator>&);
   template <class Compare>
    void merge (list<T, Allocator>&, Compare);
   void sort ();
   template <class Compare>
    void sort (Compare);
   void reverse();
};
// Non-member List Operators
template <class T>
 bool operator== (const list<T, Allocator>&, 
                  const list<T, Allocator>&);
template <class T>
 bool operator< (const list<T, Allocator>&,
                 const list<T, Allocator>&);
// Specialized Algorithms
template <class T, class Allocator>
void swap (list<T,Allocator>&, list<T, Allocator>&);

Constructors and Destructors

explicit list (const Allocator& alloc = Allocator());
explicit list (size_type n, 
               const Allocator& alloc = Allocator());
list (size_type n, const T& value, 
      const Allocator& alloc = Allocator());
template <class InputIterator>
list (InputIterator first, InputIterator last,
      const Allocator& alloc = Allocator()); 
list (const list<T, Allocator>& x);
~list ();

Assignment Operator

list<T, Allocator>& operator= (const list<T, Allocator>& x)

Allocator

allocator_type get_allocator () const;

Iterators

iterator begin ();
const_iterator begin () const;
iterator end ();
const_iterator end () const;
reverse_iterator rbegin ();
const_reverse_iterator rbegin () const;
reverse_iterator rend ();
const_reverse_iterator rend () const;

Member Functions

template <class InputIterator>
void 
assign (InputIterator first, InputIterator last);
template <class Size, class T>
void 
assign (Size n);
template <class Size, class T>
void 
assign (Size n, const T& t);
reference 
back ();
const_reference 
back () const;
void
clear ();
bool 
empty () const;
iterator
erase (iterator position);
iterator
erase (iterator first, iterator last);
reference 
front ();
const_reference 
front () const;
iterator
insert (iterator position);
iterator 
insert (iterator position, const T& x);
void 
insert (iterator position, size_type n, const T& x);
template <class InputIterator>
void 
insert (iterator position, InputIterator first,
        InputIterator last);
size_type
max_size () const;
void merge (list<T, Allocator>& x);
template <class Compare>
void 
merge (list<T, Allocator>& x, Compare comp);
void 
pop_back ();
void 
pop_front ();
void 
push_back (const T& x);
void
push_front (const T& x);
void 
remove (const T& value);
template <class Predicate>
void 
remove_if (Predicate pred);
void 
resize (size_type sz);
void 
resize (size_type sz, T c);
void 
reverse ();
size_type 
size () const;
void 
sort ();
template <class Compare>
void 
sort (Compare comp);
void 
splice (iterator position, list<T, Allocator>& x);
void 
splice (iterator position, list<T, Allocator>&  x, iterator i);
void 
splice (iterator position, list<T, Allocator >&  x,
        iterator first, iterator last);
void
swap (list <T, Allocator>& x);
void 
unique ();
template <class BinaryPredicate>
void 
unique (BinaryPredicate binary_pred);

Non-member Operators

template <class T, class Allocator>
bool operator== (const list<T, Allocator>& x,
                 const list<T, Allocator>& y);
template <class T, class Allocator>
bool operator< (const list<T, Allocator>& x,
                const list<T,Allocator>& y);
template <class T, class Allocator>
void swap (list<T, Allocator>& a, list<T, Allocator>& b);

Example

//
// list.cpp
//
 #include <list>
 #include <string>
 #include <iostream.h>
 // Print out a list of strings
 ostream& operator<<(ostream& out, const list<string>& l)
 {
   copy(l.begin(), l.end(), ostream_iterator<string>(cout," "));
   return out;
 }
 int main(void)
 {
   // create a list of critters
   list<string> critters;
   int i;
   // insert several critters 
   critters.insert(critters.begin(),"antelope");
   critters.insert(critters.begin(),"bear");
   critters.insert(critters.begin(),"cat");
   // print out the list
   cout << critters << endl;
   
   // Change cat to cougar
   *find(critters.begin(),critters.end(),"cat") = "cougar";
   cout << critters << endl;
   // put a zebra at the beginning 
   // an ocelot ahead of antelope
   // and a rat at the end
   critters.push_front("zebra");
   critters.insert(find(critters.begin(),critters.end(),
                   "antelope"),"ocelot");
   critters.push_back("rat");
   cout << critters << endl;
   // sort the list (Use list's sort function since the 
   // generic algorithm requires a random access iterator 
   // and list only provides bidirectional)
   critters.sort();
   cout << critters << endl;
   // now let's erase half of the critters
   int half = critters.size() >> 1;
   for(i = 0; i < half; ++i) {
     critters.erase(critters.begin());
   }
   cout << critters << endl;
   return 0;
 }
Output : 
cat bear antelope
cougar bear antelope
zebra cougar bear ocelot antelope rat
antelope bear cougar ocelot rat zebra
ocelot  rat zebra

Warnings

Member function templates are used in all containers provided by the Standard Template Library. An example of this feature is the constructor for list<T, Allocator> that takes two templated iterators:

template <class InputIterator>
list (InputIterator, InputIterator, const Allocator& =       Allocator());

list also has an insert function of this type. These functions, when not restricted by compiler limitations, allow you to use any type of input iterator as arguments. For compilers that do not support this feature, we provide substitute functions that allow you to use an iterator obtained from the same type of container as the one you are constructing (or calling a member function on), or you can use a pointer to the type of element you have in the container.

For example, if your compiler does not support member function templates you can construct a list in the following two ways:


int intarray[10];
list<int> first_list(intarray,intarray + 10);
list<int> second_list(first_list.begin(),first_list.end());

But not this way:


list<long> long_list(first_list.begin(),first_list.end());

since the long_list and first_list are not the same type.

Additionally, list provides a merge function of this type.

template <class Compare> void merge (list<T, Allocator>&,
  Compare);

This function allows you to specify a compare function object to be used in merging two lists. In this case, we were unable to provide a substitute function in addition to the merge that uses the operator< as the default. Thus, if your compiler does not support member function templates, all list mergers will use operator<.

Also, many compilers do not support default template arguments. If your compiler is one of these, you need to always supply the Allocator template argument. For instance, you'll have to write:

list<int, allocator>

instead of:

list<int>

See Also

allocator, Containers, Iterators


Top of document