Lec3 - Lecture 3 STL Iterators and Algorithms PIC 10C Todd...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 3: STL Iterators and Algorithms PIC 10C Todd Wittman 20.9 : Iterators s Each STL container is equipped with an iterator. s We create the iterator variable for a specific container type. container<T>::iterator variable_name; s For example, given a list<string> called L list<string>::iterator iter; iter = L.begin(); s The following pointer-like operations are defined for the iterators: s ++ -- Move the iterator forward and backward: iter++; s * Dereference to get the data: cout << *iter; s == != Compares iterator positions: if (iter1==iter2) s In the vector class only, we can move the pointer multiple steps: iter = iter + 3; iter = iter - 4;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Iterators: begin and end s Every STL container has the begin() and end() member functions which return iterators. list<string> L; list<string>::iterator iter = L.begin(); s The begin() iterator points to the first element. s But the end() iterator points to a sentinel node containing no data that is one past the last element ( past-the-end ). s Ex Print the last element in the list L above ("Maggie"). cout << *(L.end()); //NO! Gives run-time error! list<string>::iterator p = L.end(); p -- ; cout << *p; //Correct! s When a search function does not find the value it was looking for, it returns the end() iterator. "Homer" "Bart" "Lisa" "Maggie" NULL "Marge" begin() end() Multi-key Hash Table s Sometimes it useful to combine containers to get the best advantages of each container. s Suppose we want to look up records based on more than one key type. s For example, given records like. .. s We want to be able to look up the records by entering their name or age. So we want 2 search keys for each record. (We assume all keys are unique.) s But a map only lets us have only one key per record. Name
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2010 for the course PIC 10C taught by Professor Wittman during the Spring '10 term at UCLA.

Page1 / 9

Lec3 - Lecture 3 STL Iterators and Algorithms PIC 10C Todd...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online