CS11 Advanced C++ Fall Lecture 1

Similar documents
CS11 Advanced C++ Spring 2018 Lecture 2

Chapter 5. The Standard Template Library.

The Standard Template Library. An introduction

CS11 Introduction to C++ Spring Lecture 8

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

STL: C++ Standard Library

Programming in C++ using STL. Rex Jaeschke

CS197c: Programming in C++

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

Dynamic Data Structures

Exceptions, Templates, and the STL

Structuur van Computerprogramma s 2

CPSC 427a: Object-Oriented Programming

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

List, Stack, and Queues

COEN244: Class & function templates

CS 32. Lecture 5: Templates

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Lecture 4a Using The Standard Template Library. Jack Applin, Guest Lecturer

Lecture-5. STL Containers & Iterators

Unit 4 Basic Collections

Standard Template Library

CSE 100: C++ TEMPLATES AND ITERATORS

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

use static size for this buffer

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Standard Template Library. Containers, Iterators, Algorithms. Sequence Containers. Containers

Ch. 12: Operator Overloading

CSE 100: C++ TEMPLATES AND ITERATORS

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

The Ada Standard Generic Library (SGL)

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

CIS 190: C/C++ Programming. Lecture 12 Student Choice

The Standard Template Library Classes

TEMPLATES AND ITERATORS

Course Review. Cpt S 223 Fall 2009

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

G52CPP C++ Programming Lecture 18

PIC 10A. Lecture 23: Intro to STL containers

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

CSE 100: C++ TEMPLATES AND ITERATORS

Absolute C++ Walter Savitch

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CS2013 Course Syllabus Spring 2018 Lecture: Mon/Wed 2:00 P.M. 2:50 P.M. SH C259 Lab: Mon/Wed 2:50 P.M. 4:00 P.M. SH C259

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Introduction to C++ Introduction to C++ 1

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

Computational Physics

! An exception is a condition that occurs at execution time and makes normal continuation of the program impossible.

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Teaching with the STL

TDDD38 - Advanced programming in C++

Life cycle of an object construction: creating a new object. Strings: constructors & assignment another type that C and C++ don't provide

CS201 Some Important Definitions

CSC 427: Data Structures and Algorithm Analysis. Fall 2004

Course Review. Cpt S 223 Fall 2010

The C++ Standard Template Library

SFU CMPT Topic: Function Objects

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Algorithms, Data Structures, and Problem Solving

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness.

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

C and C++ 8. Standard Template Library (STL) Stephen Clark

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

COMP322 - Introduction to C++

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Templates & the STL. CS 2308 :: Fall 2015 Molly O'Neil

The C Programming Language

1. The term STL stands for?

Programming with Haiku

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Item 3: Predicates, Part 2: Matters of State

CS301 - Data Structures Glossary By

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Linked Lists and Abstract Data Structures A brief comparison

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CS 215 Lecture 17 Linked lists and time complexities

CS302. Today s Topics: More on constructor/destructor functions More on STL <list>

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

PIC10B/1 Winter 2014 Final Exam Study Guide

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

7 TEMPLATES AND STL. 7.1 Function Templates

SETS AND MAPS. Chapter 9

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Transcription:

CS11 Advanced C++ Fall 2006-2007 Lecture 1

Welcome! ~8 lectures Slides are posted on CS11 website http://www.cs.caltech.edu/courses/cs11 ~6 lab assignments More involved labs 2-3 week project at end CS Cluster Account ~/cs11/advcpp/lab1

The C++ Standard Library Sits on top of the C++ Core Language The second fundamental component of C++ Extremely useful functionality! Support for some language features memory management, runtime type information (RTTI) Portable implementations of useful functions sqrt(), memmove(), etc. (not optimized!) Nonprimitive facilities Locale support, strings, exceptions I/O streams, collections, algorithms A framework for extending these facilities

Standard Template Library (STL) Very well known part of Standard Library Primary architect: Alexander Stepanov AT&T Bell Labs, then later Hewlett Packard Andrew Koenig motivated proposal to ANSI/ISO Committee in 1994 Proposal accepted/standardized in 1994 Continuous refinements, increased support

What Is the STL? A set of generic containers, algorithms, and iterators that provide many of the basic algorithms and data structures of computer science. Generic Heavily parameterized; lots of templates! Containers Collections of other objects, with various characteristics. Algorithms For manipulating the data stored in containers. Iterators A generalization of pointers. Cleanly decouple algorithms from containers.

STL Underlying Concepts Working with STL requires fluency with: Inheritance Class templates and function templates Function pointers Basic data structures/computational complexity Some of these concepts may be a little new Don t worry; we will beat them to death.

Simple STL Example! You want an array of numbers. vector<int> v(3); // Vector of 3 elements v[0] = 7; v[1] = v[0] + 3; v[2] = v[0] + v[1]; Now you want to reverse their order! reverse(v.begin(), v.end()); vector<int> is the generic container reverse() is an algorithm reverse() uses iterators associated with v

STL Algorithms Generic function-templates Parameterized on iterator type not container Example: the find() algorithm template <class InputIterator, class T> InputIterator find(inputiterator first, } InputIterator last, const T& value) { while (first!= last && *first!= value) ++first; return first; Searches for value in range [first, last).

Algorithms and Iterators template <class InputIterator, class T> InputIterator find(inputiterator first, InputIterator last, const T& value) { InputIterator isn t a specific type! while (first!= last && *first!= value) ++first; Just needs to support * (dereference), ++ (increment), and equality operators. Pointers also satisfy these constraints. int a[5] = { 1.1, 2.3, -4.7, 3.6, 5.2 }; int *pval; pval = find(a, a + 5, 3.6); // Use int* as iterators

The Big Picture This set of required functionality for the iterator-type is called a concept. In this case, the concept is named InputIterator. A type that satisfies these requirements is said to model the concept. Or, it conforms to the concept. For example: int* is a model of Input Iterator because int* provides all of the operations that are specified by the Input Iterator requirements.

What about reverse()? The reverse() algorithm needs more! Specifically, its iterators also need -- operator. reverse() s arguments must model the BidirectionalIterator concept. Like InputIterator, but with more requirements. BidirectionalIterator refines the InputIterator concept. This is exactly like class-inheritance. Different terms because these aren t classes.

Wait A Minute A major issue with this whole concept thing: No language support whatsoever for declaring or enforcing the requirements of concepts! No language support for declaring that a particular type models a concept. This makes it a bit challenging.

Iterator Concept Hierarchy Trivial Iterator supports dereference That s it. Yep, it s trivial. Input Iterator supports increment Only read support is guaranteed. Only single-pass support guaranteed. Forward Iterator like Input Iterator Supports multi-pass algorithms. Bidirectional Iterator supports decrement Random Access Iterator Supports arbitrary-size steps forward and backward

Output Iterators Don t appear in the iterator concept hierarchy Different, very limited set of requirements Support assignment Support increment Support postincrement-and-assign It s like a tape. You can write to the current location You can advance to the next location

Function Objects Anything that can be called like a function A generalization of functions Can be a true function pointer Can be an instance of a class that overloads () Allows customization of algorithm operations Can pass these things to STL algorithms Also known as functors

Function Pointers?! C/C++ functions can be referred to by name sin(x), cos(x), sqrt(x), etc. Can also refer to functions via function pointers Like a normal pointer, but function can be called through it Function s signature is part of the pointer s type Number and types of arguments, return type Above funcs take a double and return a double A function pointer for them could be like this: double (*fp)(double); Variable name is fp Points to a function that takes a double and returns a double

Using Function Pointers Normally refer to functions to invoke them double rot = length * sin(angle); Invokes sin, using angle as argument Can also get a function s address via its name double (*fp)(double);... fp = sin; // No arguments to sin here!... double res = fp(input); Use fp like a normal function Can set fp to any function with the same signature sin, cos, tan, sqrt, log, exp, your own functions, etc.

Functor Concepts Generator f() No arguments. Unary Function f(x) One argument. Binary Function f(x, y) Two arguments. Special concept name for bool return-types Predicate bool p(x) Binary Predicate bool p(x, y) Others, too

Simple Functor Example You want a collection of 100 random values vector<int> values(100); generate(values.begin(), values.end(), rand); Can create your own functions int randomcolorvalue() { }... return rand() & 0x00FFFFFF; vector<int> randcolors(10); generate(randcolors.begin(), randcolors.end(), randomcolorvalue);

Functors with State You want to find the sum of those values Need a functor with state A class with overloaded () is perfect for this struct adder : public unary_function<int, void> { int sum; Argument Type Result Type adder() : sum(0) { } void operator()(int x) { sum += x; } }; Apply functor with for_each algorithm adder result = for_each(values.begin(), values.end(), adder()); cout << "Sum is " << result.sum << endl;

Printing The Numbers Now you want to print the numbers, separated with commas. Use copy() algorithm and Output Iterators copy(values.begin(), values.end(), ostream_iterator<int>(cout, ", ")); Note that ostream_iterator template-param must match element-type of collection.

STL Containers Sequences are a refinement of the Container Elements arranged in linear sequential order Variable size; can grow or shrink vector random access, constant append time, linear insert time, linear prepend time deque like vector, but constant prepend time too list doubly linked list, constant insert anywhere, only sequential access slist singly linked list, only forward traversal bit_vector vector of bools, optimized for space!

Associative Containers Support efficient retrieval based on keys No support for inserting at a specific position (For sequences, the key is the position) set stores keys; each appears only once map stores (key,value) pairs; each key appears only once multiset, multimap like the above, but keys can appear multiple times These are Sorted Associative Containers They don t hash the keys! Most operations are O(log(N)) But, they do keep their entries sorted by key.

Extensions to STL Containers Hashed Associative Containers have constant-time insert/retrieve operations Are considered STL Extensions hash_set, hash_map like set, map hash_multiset, hash_multimap like multiset, multimap Unlike the other Associative Containers, keys aren t kept in a specific order.

Container Adaptors Provides a restricted subset of functionality Uses another container for internal storage stack LIFO, uses deque by default queue FIFO, also uses deque by default priority_queue uses vector by default Can override the default internal container

Containers and Iterators STL containers provide iterators over their elements begin() returns an iterator to the first element end() returns an iterator just past the last element Type of element depends on the container! Sequences are simple element type is what s specified in template parameter vector<int> has elements of type int Associative containers contain (key, value) pairs map<string, int> has elements of type pair<string, int> Element type is called the container s value type

Iterator Types STL containers provide definitions of iterator types as nested typedefs vector<int> values(100);... vector<int>::iterator iter = values.begin(); while (iter!= values.end()) {... // Compute stuff. } Sometimes you need to do this If possible, use provided algorithms instead.

Helpful Resources The SGI STL Documentation http://www.sgi.com/tech/stl/index.html VERY USEFUL! Effective STL by Scott Myers