CS253: Software Development with C++

Spring 2021

Iterator Interface

Show Lecture.IteratorInterface as a slide show.

CS253 Iterator Interface

VGA, RJ-45 Ethernet, DisplayPort, USB 2.0 interfaces

Big Picture

deque<int> con = {11, 22, 33, 44, 55};
for (auto value : con)
    cout << value << ' ';
11 22 33 44 55 

Why

deque<int> con = {11, 22, 33, 44, 55};
for (auto value : con)
    cout << value << ' ';
11 22 33 44 55 
deque<int> con = {11, 22, 33, 44, 55};
for (auto it=con.begin(); it != con.end(); ++it) {
    auto value = *it;
    cout << value << ' ';
}
11 22 33 44 55 

Typical structure

    class Container {
      public:
        using value_type = whatever type this contains;
        class iterator {
          public:
            iterator(…);			      // establish a location
            value_type &operator*();		      // get value based on state
            iterator &operator++();		      // go to the next item
            bool operator!=(const iterator &) const;  // compare two iterators
          private:
            declare sufficient data to access elements in the container;
        }
        iterator begin() {
            return iterator(enough data to indicate start of container);
        }
        iterator end() {
            return iterator(enough data to indicate end of container);
        }
    };

Design

Answers, for a string

Since a string s is essentially a dynamic array of char, the answers are easy:

Different answers, for a string

Here’s another way to handle a string s:

Answers, for a list

A list is a doubly-linked list of nodes, each of which is a struct containing the data and a pointer to the next struct or nullptr.

Summary