Templates and Iterators

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
Data Structures Using C++ 2E
Templates and the STL.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Linked Lists and Generics Written by J.J. Shepherd.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advanced Programming 7/10/20161 Ananda Gunawardena.
Motivation for Generic Programming in C++
Chapter 12 Classes and Abstraction
C++ Programming:. Program Design Including
Introduction to C++ Systems Programming.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
Monday, March 19, 2018 Announcements… For Today… For Next Time…
Doubly linked lists.
Friday, February 16, 2018 Announcements… For Today… 4.5-, pgs. 252
Lab 04 – Linked List.
Object Oriented Programming COP3330 / CGS5409
Associative Structures
Chapter 18: Linked Lists.
Data structures in C++.
Linked List (Part I) Data structure.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
C++ File Structure and Intro to the STL
CSCI 3333 Data Structures Linked Lists
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked List Implementation
Recursive Linked List Operations
Classes.
STL Iterators Separating Container from Data Access.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lists - I The List ADT.
Lists - I The List ADT.
Generic Set Algorithms
The Big 5 and Lists.
C++ File Structure and Intro to the STL
Data Structures & Algorithms
Lists - I The List ADT.
Recursive Linked Lists
Doubly Linked List Implementation
Linked Lists Templates and Iterators
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Recitation Outline Hash tables in C++ STL Examples Recursive example
Lab 03 – Linked List.
The List Container and Iterators
Lab 04 - Iterator.
Generic Set Algorithms
A dictionary lookup mechanism
Presentation transcript:

Templates and Iterators

Linked List Data Structures struct ListNode { Data data_; ListNode *next_; ListNode(Data d) : data_(d), next_(nullptr) {}; } class LinkedList { ListNode *head_; … public:

What about a Linked List of something else? struct ListNode { int data_; ListNode *next_; ListNode(int d) : data_(d), next_(nullptr) {}; } class LinkedList { ListNode *head_; … public:

Better Choice Templates We have used templates such as in std::vector Templates allow for generic code that is parameterized by types. Think of them as recipes to generate code With templates all code must be included as headers are included. We use the following at the end of the header example for template version ll.h // needed for template instantiation #include "ll.cpp" } // namespace snakelinkedlist #endif //LL_H

Simple Template Function Example template<typename T> T smaller(T a, T b) { T smaller_value; smaller_value = ( a < b ) ? a : b; return smaller value; }

Now List template<typename ElementType> struct ListNode { ElementType data_; ListNode *next_; ListNode(ElementType d) : data_(d), next_(nullptr) {}; } class LinkedList { ListNode<???> *head_; … public:

Better Plan template<typename ElementType> class LinkedList { struct ListNode { ElementType data_; ListNode *next_; ListNode(ElementType d) : data_(d), next_(nullptr) {}; } ListNode<ElementType> *head_; … public:

Template Insert at start from included .cpp template<typename ElementType> void LinkedList::InsertHead(ElementType new_data) { ListNode *new_node = new ListNode(new_data); new_node->next_ = head_; head_ = new_node; }

How to access the linked list? … for(SnakeBody* curr = head_->next; curr; curr = curr->next) { ofRectangle body_rect(curr->position.x, curr->position.y, body_size_.x, body_size_.y); if (head_rect.intersects(body_rect)) { return true; }

STL style interface using range for? … for(auto snake_segment : snake_list_ ) ofRectangle body_rect(snake_segment.position.x, snake_segment.position.y, body_size_.x, body_size_.y); if (head_rect.intersects(body_rect)) { return true; }

Iterators – Simple Forward Iterator Interface Required Functions of iterator Iterator() – default constructor Iterator & operator++() – post increment as next element ElementType &operator*() – dereference operator as to access bool operator!=(const Iterator &rhs) – not equal to detect end. Required Functions of Linked List class Iterator begin() – return an iterator to the start of the list Iterator end() – return an iterator to past the end of the list

What to store in our iterator to access the list? ListNode *current_; ListNode current_; ListNode &current_; LinkedList *current_;

Iterator & operator++()

ElementType &operator*()

bool operator!=(const Iterator &rhs)

Iterator begin() Iterator begin() { }

Iterator end() Iterator end() { }