CS 225 Data Structures Sept. 29 Functors It Iterators Iterators - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Sept. 29 Functors It Iterators Iterators - - PowerPoint PPT Presentation

CS 225 Data Structures Sept. 29 Functors It Iterators Iterators give client code access to traverse the data! 8 2 5 Operators: Types of iterators: operator++


slide-1
SLIDE 1

CS 225

Data Structures

  • Sept. 29 – Functors
slide-2
SLIDE 2

It Iterators

Iterators give client code access to traverse the data! Operators: Types of iterators:

  • perator++ Forward
  • perator== Backward
  • perator!= Bidirectional
  • perator=
  • perator*

8 2 5

Ø

slide-3
SLIDE 3

#ifndef QUEUE_H #define QUEUE_H template <class QE> class Queue { public: private: }; #endif

QueueIter.h

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Where does the iterator go? What additional functions are needed for an iterator?

slide-4
SLIDE 4

#include <list> #include <string> #include <iostream> struct Animal { std::string name, food; bool big; Animal(std::string name = "blob", std::string food = "you", bool big = true) : name(name), food(food), big(big) { /* none */ } } int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::list<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::list’s insertAtEnd zoo.push_back(b); for ( std::list<Animal>::iterator it = zoo.begin(); it != zoo.end(); it++ ) { std::cout << (*it).name << " " << (*it).food << std::endl; } return 0; }

stlList.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

slide-5
SLIDE 5

template <class QE> class Queue { public:

class QueueIterator : public std::iterator<std::bidirectional_iterator_tag, T> { public: QueueIterator(unsigned index); QueueIterator& operator++(); bool operator==(const QueueIterator &other); bool operator!=(const QueueIterator &other); QE& operator*(); QE* operator->(); private: }

private: T* arr_; unsigned capacity_, count_, entry_, exit_; };

Queue.h

4 5 6

7 8 9 10 11 12 13 14 15 16 17

18 19 20 21 22 23 24 25 26

slide-6
SLIDE 6

Function Objects (aka “Functors”)

Functors are objects that can be called like a function.

1 2 3 4

slide-7
SLIDE 7

#ifndef DOUBLER_H #define DOUBLER_H class Doubler { public: int operator()(int value); }; #endif

Doubler.h

1 2 3 4 5 6 7 8 9 #include "Doubler.h" int Doubler::operator()(int value) { return value * 2; } 1 2 3 4 5

slide-8
SLIDE 8

template<class Value, class Modifier> Value modify(Value value, Modifier modifier) { return modifier(value); } 1 2 3 4 Doubler d; Tripler t; int value = 4; modify<int, Doubler>(value, d); modify<int, Tripler>(value, t); 11 12 13 14 15

slide-9
SLIDE 9

template<class Iter, class Formatter> void print(Iter first, Iter second, Formatter printer) { while ( first != second ) { printer( *first ); first++; } } 1 2 3 4 5 6 7

This is a function called __________ whose inputs are two ______________ and a ______________. This function appears to:

slide-10
SLIDE 10

#include <list> #include <string> #include <iostream> struct Animal { std::string name, food; bool big; Animal(std::string name = "blob", std::string food = "you", bool big = true) : name(name), food(food), big(big) { /* none */ } } int main() { Animal g("giraffe", "leaves", true), p("penguin", "fish", false), b("bear"); std::list<Animal> zoo; zoo.push_back(g); zoo.push_back(p); // std::list’s insertAtEnd zoo.push_back(b); for ( std::list<Animal>::iterator it = zoo.begin(); it != zoo.end(); it++ ) { std::cout << (*it).name << " " << (*it).food << std::endl; } return 0; }

stlList.cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

slide-11
SLIDE 11

Trees

slide-12
SLIDE 12

CS 225 – Things To Be Doing

Exam 4 (Programming/MP2) starts tomorrow!

More Info: https://courses.engr.illinois.edu/cs225/fa2017/exams/

MP3: Available now!

Up to +7 extra for submission by Monday, Oct. 2!

Lab: lab_quacks due Sunady, Oct 1!

Fun lab!

POTD

Every Monday-Friday – Worth +1 Extra Credit /problem (up to +40 total)