Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Day Class Notes

Download Compiler

Evening Class Notes

Lab Notes

Linux For Windows

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
The list<T> Class Template

The list<T> class template is part of the Standard Template Library.  See this web page for additional information on topics in this memo.

Note: use #include <list>

The list<T> class template is a type-independent pattern for a list.  It’s use is illustrated by the following definitions:

  list<double> doubleList;  // creates a list object with double data members
  list<string> stringList; // creates a list object with string data members

list<T> member functions

list<T> L

L.size()                Return the number of values L currently contains
L.empty()               Return true if and only if L contains no values
L.begin()               Returns an iterator pointing to first element
L.end()                 Returns an iterator pointing to beyond last element
L.push_back(value)      Append value at L’s end
L.push_front(value)     Prepend value at L's beginning
L.insert(iterator, value) Insert new value ahead of iterator position
L.pop_back()            Erase L’s last element
L.pop_front()           Erase L’s first element
L.erase(iterator)       Removes element at position iterator.  Returns an iterator
                        at the location immediately following or L.end() if the 
                        last element is removed.

L.remove(value)         Remove all occurrences of value
L.front()               Return a reference to L’s first element
L.back()                Return a reference to L’s last element

Iterators

Iterators are objects that can “point at” an element in a vector.  There are operations that permit the iterator to move from one element in the vector to another and to access the value of the element being pointed at.

begin() and end() are member functions that return iterators.  For example, L.begin() is an iterator that points to the beginning of list L.

Defining iterator objects

  list<double>::iterator listIter = L.begin();

Defines an iterator named listIter that initially points to the beginning of list L.
 
Increment operator ++  increments the iterator.  Example:  listIter++

Decrement operator --  decrements the iterator.  Example listIter—

Dereferencing operator * accesses the value stored at the position to which the iterator points. Example: *listIter

Examples:

1. List of integers

% cat link1.cpp
// STL list<T> example

#include <iostream>
using namespace std;

#include <list>

void display( list<int> );

int main()
{
   list<int> aList;

// populate list

   aList.push_back(77);
   aList.push_back(66);
   aList.push_front(88);

   cout << "Size = " << aList.size() << endl;
   display(aList);

// begin() and end() functions

   list<int>::iterator ix;
   ix = aList.begin();
   cout << "First value = " << *ix << endl;

// insert() function

   cout << "Insert 80 before 77" << endl;
   ix++;
   aList.insert(ix,80);
   display(aList);

// erase() function

   cout << "Erase 77 " << endl;
   ix = aList.erase(ix);
   display(aList);


   return 0;

}

void display(list<int> aList)
{
      for( list<int>::iterator it = aList.begin();
        it != aList.end(); it++ )
      cout << *it << endl;
}

% g++ link1.cpp
% a.out
Size = 3
88
77
66
First value = 88
Insert 80 before 77
88
80
77
66
Erase 77
88
80
66 

2. List of structs

% cat link2t.cpp
// list class example
#include <iostream>
#include <iomanip>
using namespace std;

#include <list>
#include <string>

// struct Music_Rec
struct Music_rec
{
     string comp;
     string title;
     int id;
};

void display( list<Music_rec> );

int main()
{
   list<Music_rec> musicList; 
   Music_rec r1 ={ "Bach", "Brandenburg Concerto", 234 },
             r2 ={ "Mozart", "The Magic Flute", 786 },
             r3 ={ "Stravinsky", "The Rite of Spring", 651};
 
   musicList.push_back(r1);
   musicList.push_back(r2);
   musicList.push_back(r3);

   cout << "Size = " << musicList.size() << endl;

   display(musicList);

   // insert() function

   list<Music_rec>::iterator ix = musicList.begin();
   ix++;
   Music_rec r4 ={"Prokofiev","Symphony No. 5", 793 };
   cout << "\nInsert new title" << endl;
   musicList.insert(ix,r4);
   display(musicList);

// erase() function

   cout << "\nErase Mozart" << endl;
   ix = musicList.erase(ix);
   display(musicList);

   return 0;
}

void display(list<Music_rec> musicList)
{
   cout << setiosflags(ios::left) << endl;

   for( list<Music_rec>::iterator it = musicList.begin();
        it != musicList.end(); it++ )
      cout << setw(12) << it->comp
           << setw(30) << (*it).title
           << setw(6)  << (*it).id
           << endl;
}

% g++ link2t.cpp
% a.out
Size = 3

Bach        Brandenburg Concerto          234  
Mozart      The Magic Flute               786  
Stravinsky  The Rite of Spring            651  

Insert new title

Bach        Brandenburg Concerto          234  
Prokofiev   Symphony No. 5                793  
Mozart      The Magic Flute               786  
Stravinsky  The Rite of Spring            651  

Erase Mozart

Bach        Brandenburg Concerto          234  
Prokofiev   Symphony No. 5                793  
Stravinsky  The Rite of Spring            651

  

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming