CS212: Object Oriented Analysis and Design

Slides:



Advertisements
Similar presentations
Functions Prototypes, parameter passing, return values, activation frams.
Advertisements

Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Searching Arrays Linear search Binary search small arrays
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
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
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms –Apply to a wide range of types E.g., sorting integers (long) or intervals (long, long)‏
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
1 Iterators Good reference site:
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Searching ( 搜索) 1. Introduction 2. Sequential Search 3. Binary Search 4. Comparison Trees.
Using Sequential Containers Lecture 8 Hartmut Kaiser
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
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.
Searching Arrays Linear search Binary search small arrays
CS212: Object Oriented Analysis and Design
Standard Template Library
while Repetition Structure
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
The C++ Algorithm Libraries
18 – Sequential Containers
Introduction to the Standard Template Library
CS 2308 Exam I Review.
C++ STL Vector Container
CS150 Introduction to Computer Science 1
Loops.
Lists - I The List ADT.
Lists - I The List ADT.
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Standard Template Library
Recursive Algorithms 1 Building a Ruler: drawRuler()
Functions Divide and Conquer
Standard Template Library
An Introduction to STL.
COP 3330 Object-oriented Programming in C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
ADT LIST So far we have seen the following operations on a “list”:
Standard Template Library
Presentation transcript:

CS212: Object Oriented Analysis and Design STL Algorithms

Introduction Read the content of the file Add the values together Divide by number of elements Problem statement: Suppose that we want to write a program that reads in a list of integers from a file (data.txt), then prints out the average of those values ifstream input("data.txt"); multiset<int> values; double total = 0.0; for (multiset<int>::iterator itr = values.begin(); itr != values.end(); ++itr) total += *itr; int currValue; while (input >> currValue) values.insert(currValue); cout << "Average is: " << total / values.size() << endl;

Discussion Intent In practice “read the contents of the file into the multiset“ “sum the elements together” “create an integer, and then while it's possible to read another element out of the file, do so and insert it into the multiset.” “initialize the total to zero, then iterate over the elements of the multiset, increasing the total by the value of the current element at each step.” “Mechanical model”

Accumulate Averaging program Using “accumulate” algorithm double total = 0.0; for (multiset::iterator itr = values.begin(); itr != values.end(); ++itr) total += *itr; cout << "Average is: " << total / values.size() << endl; Using “accumulate” algorithm cout << accumulate(values.begin(), values.end(), 0.0) / values.size() << endl;

More on “accumulate” Defined in the <numeric> header Three parameters (arguments) are passed to it Iterator 1: Beginning of the range of values Iterator 2: End of the range of values Initial value to be used in the summation Demonstration (AccumSum.cpp) template <class InputIterator, class T> T accumulate (InputIterator first, InputIterator last, T init);

Do we really need them? There are reasons behind using STL algorithms Simplicity Leverage existing codes A great time-saver Correctness Reduces the chance of slip up and making mistakes Speed Optimized to work as fast as possible Clarity Ease of understanding of existing modules

Reordering algorithms Reorder but preserve the elements in a container sort(RandomAccessIterator first, RandomAccessIterator last ); random_shuffle(RandomAccessIterator first, RandomAccessIterator last)); rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); Demonstration (ReorderAlgo.cpp)

Searching Algorithms Checking membership in a container InputIterator find (InputIterator first, InputIterator last, const T& val); Returns an iterator to the first element in the range that compares equal to val If no such element is found, the function returns last. bool binary_search (ForwardIterator first, ForwardIterator last, const T& val); The elements in the range shall already be sorted Demonstration (SearchAlgo.cpp) 

Removal Algorithms For removing elements from containers Removal algorithms do not actually remove elements from containers Algorithms accept iterators, not containers Do not know how to erase elements from containers Removal functions work by shuffling down the contents of the container

Removal algorithm: example 1 2 3 4 3 ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); The removal is done by replacing the elements that compare equal to val by the next element that does not F L 1 2 3 4 4 == == == == == == == 3 3 3 3 3 3 3 Demonstration (RemoveAlgo.cpp)