Lecture 27Outlinelist<T>Using list<T> 1Using list<T> 2Iterators 1Iterators 2Iterators 3Iterator Variables 1Iterator Variables 2Getting an Iteratorinsert( ) / erase( ) 1insert( ) / erase( ) 2insert( ) / erase( ) 3In-class Exercise 1Iterator ClassesSTL <algorithm> LIbrary 1STL <algorithm> LIbrary 2In-class Exercise 2Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 1Lecture 27Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture27/*.*Reminder: Project 5 due next Wednesday. If you haven't started, you're already late!Questions?Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 2Outlinelist<T> data structureMost of the material on list<T> is not in the textbookIteratorsAlso covered on pages 140-142 with respect to the multiset<T> data structure (which is the STL bag type)STL <algorithm> libraryWednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 3list<T>As noted previously, list<T> is a general-purpose container in the STL that is implemented using a doubly-linked list.It is defined in the <list> library, and it is in namespace std.It has all the vector<T> operations except operator[ ]. In addition, it has push_front( ), pop_front( ), and front( ) that operate on the front (i.e., head) end of the list.Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 4Using list<T>With just these operations, we can do a few (possibly) interesting things. E.g., we can write a function that receives a list<T> and returns true if the items in the list are palindromic order; false otherwise. That is, the list prints out the same forwards and backwards.Since we currently are limited to accessing the ends, the only way to do this is to make a copy of the received list, then compare the ends, popping them off if they are equal.Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 5Using list<T>See list-examples.cpp and list-templates.htemplate <typename T>bool IsPalindrome (const list<T> & aList){ list<T> copyList = aList; // make a copy while (copyList.size() > 1) // more elem's { // compare ends if (copyList.front() != copyList.back()) return false; copyList.pop_front(); copyList.pop_back(); } // end while return true; // all matched}Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 6IteratorsOf course, we would like to be able to scan through the list. Unlike a vector, there is no way to do so using operator[ ]. We need to be able to "point" to an element in the list.Recall for arrays we can say:const int ARRSIZE = 10;int a[ARRSIZE];for (int *aPtr = a; aPtr != a+ARRSIZE; aPtr++) cout << *aPtr << endl;aPtr[0][1]a:Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 7IteratorsGeneralize the notion of a pointer to an iterator. An iterator uses the same syntax as a pointer:* to dereference iterator and obtain the pointee++ and -- to move to next/previous element in collection== and != to compare if pointing to the same elementWednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 8IteratorsPicture is similar for a list<T>:To print out the element pointed to by cursor:cout << *cursor << endl;To advance cursor to the next element:cursor++;12 3 7 3 12intList2cursorWednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 9Iterator VariablesEach STL container type defines iterator and const_iterator (for constant collections) as a type inside the class template. In a non-template function (e.g., main) where the type parameter is instantiated, this means that the type needs to be prefixed with the class name and scope operator as usual:list<int>::iterator cursor;list<string>::const_iterator strListIter;Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 10Iterator VariablesInside a template function, iterator variable declaration also must be prefixed with the typename keyword.template <typename T>void PrintList (const list<T> & aList){ typename list<T>::const_iterator cursor; :Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 11Getting an IteratorEach STL container type defines operations begin( ) and end( ) that return an iterator to the first element of the collection and an iterator to one past the last element of the collection, respectively.This generalizes the notion that a range is [first, last). Then scanning becomes: for (cursor = aList.begin(); cursor != aList.end(); cursor++) { // Do something with *cursor }Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 12insert( ) / erase( )In addition to scanning a collection, iterators are used to insert or erase (i.e. remove) elements from (the middle of) collections. For inserting, the new element is inserted in front of the element the iterator argument points to and returns an iterator to the new element.list<int>:: iterator cursor, newEltcursor = intList.begin();cursor++; // insert between 1st & 2nd elemnewElt = intList.insert(cursor, 14); // newElt "points" to new elementWednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 13insert( ) / erase( )For erasing, the iterator argument points to the element to be removed. An iterator to the element after the removed one is returned. This result usually is assigned to the argument iterator, as it is no longer valid after its element is erased.cursor++; // move to the next elementcursor = intList.erase(cursor); // now points to the element after thatCan also erase a range [first, last):intList.erase (intList.begin(), intList.end()); // same as intList.clear()Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 14insert( ) / erase( )Note: Since vector<T> is an STL container, iterators are available for a vector and the insert and erase operations are defined as well.However, as discussed previously, inserting and removing items from the middle of a vector are O(n) operations, so should be avoided.Wednesday, October 27 CS 215 Fundamentals of Programming II - Lecture 27 15In-class ExerciseIn file list-templates.h, write a template function RemoveDuplicates that receives and passes back a list<T>. The function should remove any duplicates in the list.In file list-examples.cpp, write code to use RemoveDuplicates to remove duplicates from intList2 and then
View Full Document