Using Sequential Containers Lecture 8 Hartmut Kaiser

Slides:



Advertisements
Similar presentations
The List Type Lecture 9 Hartmut Kaiser
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Beginning C++ Through Game Programming, Second Edition
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Lists Introduction to Computing Science and Programming I.
Algorithm Efficiency and Sorting
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Hashing General idea: Get a large array
Basic C++ Sequential Container Features
ECE122 L17: Method Development and Testing April 5, 2007 ECE 122 Engineering Problem Solving with Java Lecture 17 Method Development and Testing.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Data Structures Using C++ 2E
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Organizing Programs and Data Lecture 5 Hartmut Kaiser
Containers Overview and Class Vector
Working with Strings Lecture 2 Hartmut Kaiser
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
The List Type Lecture 9 Hartmut Kaiser
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
Defining New Types Lecture 21 Hartmut Kaiser
SE: CHAPTER 7 Writing The Program
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Working with Batches of Data Lecture 4 Hartmut Kaiser
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Looping and Counting Lecture 3 Hartmut Kaiser
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Taking Strings Apart (and putting them together) Lecture 11 Hartmut Kaiser
Using Library Algorithms Lectures 12 and 13 Hartmut Kaiser
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Using Associative Containers Lecture 18 Hartmut Kaiser
Writing Generic Functions Lecture 20 Hartmut Kaiser
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Searching Topics Sequential Search Binary Search.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Motivation and Overview
C++ Standard Library.
The Pseudocode Programming Process
Defining New Types Lecture 22 Hartmut Kaiser
The List Type Lecture 9 Hartmut Kaiser
Data Structures Introduction
Presentation transcript:

Using Sequential Containers Lecture 8 Hartmut Kaiser

Programming Principle of the Day Principle of least astonishment (POLA/PLA)  The principle of least astonishment is usually referenced in regards to the user interface, but the same principle applies to written code.  Code should surprise the reader as little as possible. That means following standard conventions, code should do what the comments and name suggest, and potentially surprising side effects should be avoided as much as possible. 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 2

Abstract We start looking beyond vector and string. We will focus on sequential containers and demonstrate a couple of problems we can solve when applying them. The standard library’s architecture will start to get visible. That will help us to start understanding how to use all of the different containers in the standard library. 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 3

Separating Students into Categories Sort out failed students  Who failed?  Remove from our data Create a new vector of student_data containing only students who succeeded: // predicate to determine whether a student failed bool fail_grade(student_info const& s) { return grade(s) < 60; } Push student data onto one of two containers based on this predicate 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 4

Separating Students into Categories What’s wrong here? (Hint: what’s the memory consumption?) // separate passing and failing student records: first try vector extract_fails(vector & students) { vector pass, fail; for (vector ::size_type i = 0; i != students.size(); ++i) { if (fail_grade(students[i])) fail.push_back(students[i]); else pass.push_back(students[i]); } students = pass; return fail; } 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 5

Separating Students into Categories Requires twice as much memory  Each record is held twice Better to copy failed students, removing the data from original vector  How to remove elements from a vector?  Slow, too slow for larger amounts of data.  Why?  What happens if all students have failed?  This can be solved by either using a different data structure or by modifying the algorithm 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 6

Erasing Elements in Place Slow, but direct solution (Why is it slow?) // second try: correct but potentially slow vector extract_fails(vector & students) { vector fail; vector ::size_type i = 0; // invariant: elements [0, i) of students represent passing grades while (i != students.size()) { if (fail_grade(students[i])) { fail.push_back(students[i]}; students.erase(students.begin() + i); } else ++i; } return fail; } 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 7

Erasing Elements in Place The erase() function takes a special type ‘pointing’ (referring) to the element to erase: students.erase(students.begin() + i); 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 8 Elements we’ve already seenFAIL Elements we haven’t processed Element i students.size() == n Elements we’ve already seen Elements we haven’t processed students.size() == n - 1 (These elements are copied)

Erasing Elements in Place Caution: why will this fail? // this code will fail because of misguided optimization auto size = students.size(); while (i != size) { if (fail_grade(students[i])) { fail.push_back(students[i]); students.erase(students.begin() + i); } else ++i; } 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 9

Sequential Versus Random Access Both versions share a non-obvious property  The elements are accessed sequentially only  We used integer ‘i’ as an index, which hides that  Need to analyze every operation on ‘i’ to verify  We might access student data in arbitrary order Every container type has its performance characteristics for certain operations  By knowing what access pattern we use we can utilize the ‘best’ container type 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 10

Sequential Versus Random Access Let’s restrict our access to being sequential The standard library exposes special types we can use to express this intent: Iterators  By choosing the right type of iterator we ‘tell’ the library what access pattern we need to support  Allows for optimal selection of the underlying algorithm implementation 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 11

Iterators Our code uses the index for  Access of an element fgrade(students[i])  Move to the next element (increment ‘i’) while (i != students.size()) { // work gets done here; but doesn't change the value of i i++; } We use index for sequential access only! But there is no way of telling the library about this 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 12

Iterators Iterators are special types  Identify a container and an element in the container  Let us examine the value stored in that element  Provide operations for moving between elements in the container  Restrict the available operations in ways that correspond to what the container can handle efficiently 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 13

Iterators Code using iterators is often analogous to index based code: // code based on indicies for (vector ::size_type i = 0; i != students.size(); ++i) { cout << students[i].name << endl; } // code based on iterators for (vector ::const_iterator iter = students.begin(); iter != students.end(); ++iter) { cout << (*iter).name << endl; } 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 14

Iterator Types Every standard container, such as vector, defines two associated iterator types: container_type::iterator container_type::const_iterator  Where container_type is the container (vector )  Use iterator to modify the element, const_iterator otherwise (read only access) Note, that we don‘t actually see the actual type, we just know what we can do with it.  Abstraction is selective ignorance! 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 15

Iterators, C++11 Code using iterators is often analogous to index based code: // code based on indicies for (auto i = 0; i != students.size(); ++i) { cout << students[i].name << endl; } // code based on iterators using C++11 (VS2010, g++4.2) for (auto iter = students.begin(); iter != students.end(); ++iter) { cout << (*iter).name << endl; } 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 16

Iterator Types Every container_type::iterator is convertible to the corresponding container_type::const_iterator  students.begin() returns an iterator, but we assign it to a const_iterator Opposite is not true! Why? 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 17

Iterator Operations Containers not only expose their (specific) iterator types, but also actual iterators: students.begin(), students.end()  begin(): ‘points’ to the first element  end(): ‘points’ to the element after the last one Iterators can be compared : iter != students.end()  Tests, whether both iterators refer to the same element Iterators can be incremented : ++iter  Make the iterator ‘point’ (refer) to the next element 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 18

Iterator Operations Iterators can be dereferenced : *iter  Evaluates to the element the iterator refers to In order to access a member of the element the iterator refers to, we write: (*iter).name  (why not: *iter.name ?) Syntactic sugar, 100% equivalent: iter->name 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 19

Iterator Operations Some iterators can get a number added students.erase(students.begin() + i);  Overloaded operator+, makes the iterator refer to the ‘i’ –s element after begin  Equivalent to invoking ++ ‘i’ times  Defined only for iterators from random access containers  vector, string are random access (indexing is possible)  Will result in compilation error for sequential containers 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 20

Erasing Elements in Place Slow, but direct solution // second try: correct but potentially slow vector extract_fails(vector & students) { vector fail; auto i = 0; // invariant: elements [0, i) of students represent passing grades while (i != students.size()) { if (fail_grade(students[i])) { fail.push_back(students[i]}; students.erase(students.begin() + i); } else ++i; } return fail; } 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 21

Erasing Elements in Place Still slow, but without indexing: // version 3: iterators but no indexing vector extract_fails(vector & students) { vector fail; auto iter = students.begin(); while (iter != students.end()) { if (fail_grade(*iter)) { fail.push_back(*iter); iter = students.erase(iter); // watch out! Why? } else ++iter; } return fail; } 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 22

Iterator Invalidation What happens to an iterator if the element it refers to is deleted?  It is invalidated  Certain containers invalidate all iterators after the deleted element as well (vectors) For that reason erase() returns the next iterator: iter = students.erase(iter); 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 23

Same problem as Before Why does this code fail: // this code will fail because of misguided optimization auto iter = students.begin(); auto end_iter = students.end(); while (iter != end_iter) { //... } End iterator is invalidated as well when element is erased! 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 24

What’s the problem with vector For small inputs, vector works just fine, larger inputs cause performance degradation  Vector is optimized for fast access to arbitrary elements and for fast addition to the end  Inserting or removing from the middle is slow.  All elements after the inserted/removed element need to be moved in order to preserve fast random access  Our algorithm has quadratic performance characteristics  Let’s utilize a different data structure:  Next lecture: The list type 19/2/2015, Lecture 8 CSC 1254, Spring 2015, Using Sequential Containers 25