Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.

Slides:



Advertisements
Similar presentations
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Vectors, lists and queues
. 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.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
More on the STL vector list stack queue priority_queue.
1 Standard Containers: Lists Gordon College Resource:
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Templates and the STL.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms –Apply to a wide range of types E.g., sorting integers ( int ) vs. intervals ( pair )
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
February 4, 2005 Searching and Sorting Arrays. Searching.
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)‏
Functional Programming Think Differently!! Functional languages are expressive, accomplishing tasks using short, succinct and readable code. Functional.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Standard Template Library (STL) ~ Generic Programming.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Iterators Good reference site:
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
Lecture 7 : Intro. to STL (Standard Template Library)
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
1 The Standard Template Library Drozdek Section 3.7.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Introduction to Templates and Standard Template Library 1.
Lecture 36 OOP The STL Standard Template Library
C++ Standard Template Library
Concepts of Programming Languages
Regarding homework 9 Many low grades
Lecture 7-2 : STL Iterators
Generic Programming Techniques in C++
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Lecture 7-3 : STL Algorithms
18 – Sequential Containers
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
C++ Functions, Classes, and Templates
Introduction to C++ STL
priority_queue<T>
Elements are always copied when they are put into a container
Lecture 8 : Intro. to STL (Standard Template Library)
Lecture 8-2 : STL Iterators and Algorithms
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
An Introduction to STL.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Standard Template Library
Presentation transcript:

Lecture 8-3 : STL Algorithms

STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence containers. To use them, we must write #include in our program. In this lesson we will see a few different algorithms contained in the STL (for others see the textbook): sort() find(), find_if() for_each() transform(), copy() count_if()

Example: STL Algorithm –sort() Let vector A; for some class T. Let vector ::iterator p, q sort(p, q) sorts A between p and q. Common case is sort(A.begin(), A.end()); sorts all of A.

example // sort without comparators #include using namespace std; template void Display(Iterator start, Iterator end) { for( Iterator p = start; p != end; ++p ) cout<< *p << “ “; } int main() { vector composer; composer.push_back(“Mozart”); composer.push_back(“Bach”); composer.push_back(“Chopin”); composer.push_back(“Beethoven”); cout<< “composer: “; Display(composer.begin(), composer.end()); cout<< endl; sort(composer.begin(), composer.end()); cout<< “composer: “; Display(composer.begin(), composer.end()); cout<< endl; vector v; for (int i = 1; i < 13; i++) { v.pushback(i*i % 13); } cout<< “v: “; Display(v.begin(), v.end()); cout<< endl; sort(v.begin(), v.end()); cout<< “v: “; Display(v.begin(), v.end()); cout<< endl; } Output composer: Mozart Bach Chopin Beethoven composer: Bach Beethoven Chopin Mozart v: v:

Example: STL Algorithm –sort() sort() also works with deque objects but not with list objects. In general, sort() works with any random access sequence container. Guaranteed O(n log n) running time.

Another Example: STL Algorithm – find() #include int main() { list composer; composer.push_back(“Mozart”); composer.push_back(“Bach”); composer.push_back(“Chopin”); composer.push_back(“Beethoven”); list ::iterator p; p = find(composer.begin(), composer.end(), “Bach”); if (p == composer.end()) { cout << “Not found.” << endl; } else if (++p != composer.end()) { cout<< “Found before: “ << *p << endl; } else { cout<< “Found at the end.” << endl; }

Algorithms, Iterators, and Sub-sequences Sequences/Sub-sequences are specified using iteratorsthat indicate the beginning and the end for an algorithm to work on. Here we find the 2 nd occurrence of the value, 341, in a sequence. // File “init.cpp” int f(int x) { return -x*x + 40*x + 22; } // // template void my_initialization(T& x) { const int N = 39; for (int j = 0; j < N; ++j) { x.push_back( f(j) ); }

Example #include #include ''init.cpp'' int main() { const int search_value= 341; vector x; my_initialization(x); vector ::iteratorp; p = find(x.begin(), x.end(), search_value); if (p != x.end()) {// Value found! p = find(++p, x.end(), search_value); // Find again if (p != x.end()) {// Value found again! cout<< “Found after: “ << *--p << endl; }

STL find() –‘Implementation’ find() searches linearly through a sequence, and stops when an item matches the 3rd argument. A big limitation of find() is that it requires an exact match by value. template IteratorT find(IteratorT first, IteratorT last, const T& value) { while (first != last && *first != value) { ++first; } return first; }

find_if Returns an iterator to the first element in the range [first,last) for which applying f to it, is true. // find_if example #include using namespace std; bool IsOdd (int i) { return ((i%2)==1); } int main () { vector myvector; vector ::iterator it; myvector.push_back(10); myvector.push_back(25); myvector.push_back(40); myvector.push_back(55); it = find_if (myvector.begin(), myvector.end(), IsOdd); cout << "The first odd value is " << *it << endl; return 0; } Output : The first odd value is 25

for_each Applies function f to each of the elements in the range [first,last). #include using namespace std; void myfunction (int i) { cout << " " << i; } int main () { vector myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myfunction); cout << endl; return 0; } Output : myvector contains:

copy Copies the elements in the range [first,last) into a range beginning at result. Returns an iterator to the last element in the destination range. // copy algorithm example #include using namespace std; int main () { int myints[]={10,20,30,40,50,60,70}; vector myvector; vector ::iterator it; myvector.resize(7); // allocate space for 7 elements copy ( myints, myints+7, myvector.begin() ); cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0; } Output : myvector contains: