Lecture 7-3 : STL Algorithms

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

Vectors, lists and queues
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
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.
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Templates and the STL.
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
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.
February 4, 2005 Searching and Sorting Arrays. Searching.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Standard Template Library (STL) ~ Generic Programming.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Lecture 7 : Intro. to STL (Standard Template Library)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 The Standard Template Library Drozdek Section 3.7.
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
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.
Searching Arrays Linear search Binary search small arrays
Lecture 36 OOP The STL Standard Template Library
Motivation for Generic Programming in C++
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Concepts of Programming Languages
Regarding homework 9 Many low grades
Generic Algorithms (TIC++V2:C6)
Lecture 7-2 : STL Iterators
CSCE 210 Data Structures and Algorithms
STL – Standard Template Library
Basic Data Structures.
Sorting Algorithms.
C++ Programming Standard Library
Generic Programming Techniques in C++
Starting Out with C++ Early Objects Eighth Edition
The C++ Algorithm Libraries
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 9 – Sets and Maps 9.1 Associative Container
Basic Data Structures.
Chapter 22: Standard Template Library (STL)
C++ Templates L03 - Iterator 10 – Iterator.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
C++ Functions, Classes, and Templates
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Introduction to C++ STL
priority_queue<T>
Chapter 9 One-Dimensional Arrays
Standard Template Library Find
Elements are always copied when they are put into a container
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
C++ Templates L03 - Iterator 10 – Iterator.
Lecture 8-2 : STL Iterators and Algorithms
STL Библиотека стандартных шаблонов
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.
Chapter 9 – Sets and Maps 9.1 Associative Container
Standard Template Library
Presentation transcript:

Lecture 7-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 <algorithm> 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() Etc binary_search, count, min/max, swap, partition, rotate set_difference, set_union, set_intersection

Example: STL Algorithm –sort() Let vector<T> A; for some class T. Let vector<T>::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 Output composer: Mozart Bach Chopin Beethoven // sort without comparators #include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; template<class Iterator> void Display(Iterator start, Iterator end) { for( Iterator p = start; p != end; ++p ) cout<< *p << “ “; } int main() vector<string> 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()); vector<int> 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()); Output composer: Mozart Bach Chopin Beethoven composer: Bach Beethoven Chopin Mozart v: 1 4 9 3 12 10 1012 3 9 4 1 v: 1 1 3 3 4 4 9 9 10 1012 12

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.

See also sort() for user defined types example : stl_sort.cpp

Another Example: STL Algorithm –find() #include <algorithm> #include <string> #include <list> int main() { list<string> composer; composer.push_back(“Mozart”); composer.push_back(“Bach”); composer.push_back(“Chopin”); composer.push_back(“Beethoven”); list<string>::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 2nd occurrence of the value, 341, in a sequence. // File “init.cpp” int f(int x) { return -x*x + 40*x + 22; } // 22 61 98 133 166 197 226 253 278 301 322 341 358 373 386 397 // 406 413 418 421 422 421 418 413 406 397 386 373 358 341 322 301 template<typename T> void my_initialization(T& x) { const int N = 39; for (int j = 0; j < N; ++j) { x.push_back( f(j) ); }

Example #include <vector> #include <algorithm> #include ''init.cpp'' int main() { const int search_value= 341; vector<int> x; my_initialization(x); vector<int>::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’ equivalent to : 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 <class IteratorT, class T> IteratorT Find( IteratorT begin, IteratorT end, const T& value ) { for ( ; begin != end ; begin++) if (*begin == value) break; return begin; }

find() for user defind type operator== is necessary (no necessary for built-in type) #include <algorithm> #include <vector> #include <iostream> using namespace std; class mytype { public : int key; int height; int weight; mytype(int k, int h, int w) : key(k) , height(h) , weight(w){} bool operator==(const int a) if (key==a) return true; return false; } }; int main() { vector<mytype> vec; mytype x1(1,172,70); mytype x2(2,163,50); mytype x3(3,178,74); mytype x4(4,185,80); vec.push_back(x1); vec.push_back(x2); vec.push_back(x3); vec.push_back(x4); for (int i=0;i<vec.size();i++) { cout << vec[i].key << " “ << vec[i].height << " “ << vec[i].weight << endl; } vector<mytype>::iterator p; p=find(vec.begin(),vec.end(),3); cout << "found: " << p->key << " " << p->height << " " << p->weight << endl; return 1;

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 <iostream> #include <algorithm> #include <vector> using namespace std; bool IsOdd (int i) { return ((i%2)==1); } int main () { vector<int> myvector; vector<int>::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

Many other useful <algorithm> functions…