Jim Fawcett CSE687 – Object Oriented Design Spring 2002

Slides:



Advertisements
Similar presentations
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Mutating Algorithms All the algorithms we have seen have not modified the containers Some algorithms do modify values They do not modify iterators, only.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
What is generic programming? The essence of the generic programming approach is concept development: systematic classification of computing components.
Concept= a set of abstractions (e.g., types) (Generic Programming) Programming with Concepts defined by a set of requirements {vector, deque, list, set,
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
STL Algorithms algorithms independent of containers.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)
Generic Programming Using the C++ Standard Template Library.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
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.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
1 Iterators Good reference site:
The Utility Class pair All associative containers make use of the pair template, which is defined in. Slightly stripped down, it looks basically like this:
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
An Introduction to STL - Standard Template Library
Motivation for Generic Programming in C++
Jim Fawcett CSE 691 – Software Modeling and Analysis Fall 2000
CS212: Object Oriented Analysis and Design
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2005.
Jim Fawcett CSE687 – Object Oriented Design Spring 2010
Standard Template Library
Programming with ANSI C ++
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Chapter 4 Linked Lists.
Jim Fawcett Copyright ©
Template Policies and Traits By Example
Starting Out with C++ Early Objects Eighth Edition
Chapter 9 – Sets and Maps 9.1 Associative Container
Programming with Concepts
Chapter 22: Standard Template Library (STL)
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
CS212: Object Oriented Analysis and Design
Containers, Iterators, Algorithms, Thrust
Elements are always copied when they are put into a container
Recitation Outline C++ STL associative containers Examples
Standard Version of Starting Out with C++, 4th Edition
Iterators and STL Containers
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library
the Standard Template Library
Standard Template Library
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Template Policies and Traits By Example
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Jim Fawcett Copyright ©
Standard Template Library
Presentation transcript:

Jim Fawcett CSE687 – Object Oriented Design Spring 2002 STL Odds and Ends Jim Fawcett CSE687 – Object Oriented Design Spring 2002

Removing Element Values To remove all values, t, from a vector, v, use: v.erase(remove(v.begin(),v.end(),t),v.end()); To remove all values, t, from a list, l, use: l.remove(t); // note: this remove is a member function To remove all values, t, from an associative container, c, use: c.erase(t); // remove corrupts an associative container

Finding and Counting Values What do you want to know? Algorithm to Use Member function to use Unsorted range Sorted Set or map Multiset or multimap Does the desired value exist? find binary_search count Does the desired value exist? If so, where is the first object with that value? equal_range or lower_bound Where is the first object with a value not preceding the desired value? find_if Where is the first object with a value succeeding the desired value? upper_bound How many objects have the desired value? Where are all the objects with the desired value? find (iteratively) Effective STL, Scott Meyers, Addison Wesley, 2001

Functor Notes STL Algorithms pass functors by value, so all your functor designs should properly support copying and assignment, perhaps by simply allowing compiler to generate those operations. Predicate functors should be pure functions, e.g., they always return a boolean result as a function of their argument(s) only, e.g., internal state plays no role in determining the return value. The usual way to do this is simply to avoid giving predicate functors any state or declare them const.

Code Examples Inserter – demonstrates use of transform algorithm using an inserter iterator. Sorting – demonstrates use of sort, find_if, upper_bound, and lower_bound