Comp 2673, Spring 2002
May 8, lecture notes

Reading assignment: Read about iterators and lists in D&D Chapter 20 Here's a simple program that creates a vector and traverses through the vector: vectortest1.cpp. The method of traversing: for (i=0; i < ivec.size(); i++) is a primitive way to use the vector class. A more sophisticated method is to use iterators. An iterator is a smart pointer that allows you to traverse through an stl container without necessarily having knowledge of how that container works. Examine this program, which shows how to define iterators, how to increment them, and how to use them to traverse through a vector: vectortest2.cpp Like the stl vector class implements a smart array, there is also an stl container that implements a linked list. This linked-list container is called "list". To gain access to it, #include <list> A linked list holds a sequential list of objects just as a vector does, but it is implemented in a completely different way. The items in the list are no longer necessarily stored contiguously in memory, but rather each element has a pointer that indicates where to find the next element and the previous element in the list. Linked lists are efficient when insertions and/or deletions are regularly made in the middle of the list. By using an iterator to traverse the list, we can bypass having to access these next and previous pointers - incrementing an iterator in a list automatically makes the iterator follow the pointers to the next element. Note that the < operator and > operator are not available for lists - use the != operator instead. Examine this program to see how to create a list, insert items into the list, traverse the list, and access elements of the list. listtest.cpp