OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
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,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
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)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
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.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Writing Your Own STL Container Ray Lischner
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
Containers Overview and Class Vector
Containers and Iterators CNS 3370 Copyright 2003, Fresh Sources, Inc.
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms Slide
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Functional Programming Think Differently!! Functional languages are expressive, accomplishing tasks using short, succinct and readable code. Functional.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
The C++ Standard Template Library. What is STL A subset of the standard C++ library –The string class is not part of it! 3 major components –Algorithms.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
1 Iterators Good reference site:
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
A gentle introduction to the standard template library (STL) Some references:
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
The Standard Template Library Container Classes Version 1.0.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
C++ Review STL CONTAINERS.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
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.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
Introduction to olympic programming
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Object Oriented Programming COP3330 / CGS5409
C++ STL Vector Container
Elements are always copied when they are put into a container
Level 1 STL – Linear DS fb.com/acmicpcfcicu. Static Arrays Creating 1D, 2D, ND Static Arrays. Accessing Time. Traversing a static array with N dimensions.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Presentation transcript:

OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10

OOP Etgar 2008 – Recitation 102 A First Glimpse into STL

OOP Etgar 2008 – Recitation 103 The STL The STL is a generic library that provides efficient data structures and modern algorithms to work with them. There are 3 main parts: –Containers manage collections of objects ( list, vector, queue, etc.) –Iterators are objects used to access elements of containers. –Algorithms are used to process elements of containers (sort, find, merge, etc.).

OOP Etgar 2008 – Recitation 104 Introducing Iterators

OOP Etgar 2008 – Recitation 105 Accessing Container Elements When we are using containers (data structures), we need a way to access elements within the container. With built-in arrays we use index and [] to access any element. But when we use generic containers we need a “generic” way to access elements.

OOP Etgar 2008 – Recitation 106 Iterators An iterator is such a generic way – it is an object that lets us examine elements in a container and navigate from one element to another. –A generalization of index+ [] for built-in arrays.

OOP Etgar 2008 – Recitation 107 Iterator Types Each container has its own iterator type. –There is an iterator type for vector s, another iterator type for list s, and so on. The exact type of the iterator for a container is defined as a static member in that container called iterator : vector ::iterator it_vec; it_vec above is an object that is used to access elements of a vector.

OOP Etgar 2008 – Recitation 108 begin() and end() To use it_vec to access elements of a container, we need to “initialize” it with a container. vector vec; it_vec = vec.begin(); Containers provide begin() and end() methods, that return iterators to the first and one-past-last elements of the container.

OOP Etgar 2008 – Recitation 109 Basic Usage Iterators are designed to behave just like pointers – an iterator “points” to an element of the container. –Element access is done using *. –Next element is fetched with ++ (prefer prefix). –Can be compared with ==.

OOP Etgar 2008 – Recitation 1010 Example int main() { vector v(10);// Vector with 10 elements vector ::iterator iter;// Iterator to vector // Zero out all v's elements for (iter = v.begin(); iter != v.end(); ++iter) { *iter = 0; } // Sort v's elements sort( v.begin(), v.end() ); return 0; }

OOP Etgar 2008 – Recitation 1011 Iterator Categories It’s easy to see there are several categories of iterators. – vector s can be randomly accessed, so vector iterators should allow iterator arithmetics. –But this will be too expensive for list s. –Different algorithms require different iterator categories. Main iterator categories used in STL are: –Input/Output (Only read/write forward); –Forward (Read and write forward); –Bidirectional (Read and write in both directions); –Random access (Read and write in any place);

OOP Etgar 2008 – Recitation 1012 Containers

OOP Etgar 2008 – Recitation 1013 Sequence Container vector The vector represents a dynamic array: –Random access to elements. –Appending/removing elements at the end is fast. –Inserting/removing in the middle is slow. –Iterator category: Random. Main operations: –Random access ( [], at() ); Stack operations ( push_back(), pop_back() ); List operations ( insert(pos,elem), erase(pos) ); Standard ( front(), back(), size(), empty(), swap(), == ). Declared in header.

OOP Etgar 2008 – Recitation 1014 vector Example #include using namespace std; int main() { vector v; for (int i=1; i<=6; ++i) { v.push_back(i); } for (vector ::size_type i=0; i<v.size(); ++i) { cout << v[i] << ' '; } vector ::iterator p = find( v.begin(), v.end(), 3 ); v.erase(p); }

OOP Etgar 2008 – Recitation 1015 Sequence Container list The list represents a doubly-linked list: –Insertion/removal at any position is fast. –Random access is not possible. –Iterator category: Bidirectional. Main operations: –List operations ( insert(pos,elem), erase(pos) ); Front operations ( push_front(), pop_front() ); Stack operations ( push_back(), pop_back() ); Standard ( front(), back(), size(), empty(), swap(), == ). Declared in header.

OOP Etgar 2008 – Recitation 1016 list Example #include using namespace std; int main() { list l; for (char c='a'; c<='z'; ++c) { l.push_back(c); } list ::iterator p = max_element( l.begin(), l.end() ); cout << "Max element - " << *p << '\n'; while ( ! l.empty() ) { cout << l.front() << ' '; l.pop_front(); }

OOP Etgar 2008 – Recitation 1017 Associative Container map The map contains key/value pairs: –The elements (pairs) are stored sorted (by default, using operator<() ) –No front/stack operations, but list operations are fast. –Elements are accessed by the key half of the pair. –The keys must be unique. –Iterator category: Bidirectional. Main operations: –Access by key ( [] ); List operations ( insert(elem), erase(elem) ); Standard ( size(), empty(), swap(), == ). Declared in header.

OOP Etgar 2008 – Recitation 1018 map Example #include using namespace std; int main() { map m; m.insert(make_pair(2,"have")); m.insert(make_pair(1,"We")); m.insert(make_pair(4,"map.")); m.insert(make_pair(3,"a")); map ::iterator p; for (p = m.begin(); p != m.end(); ++p) { cout second << ' '; } cout << "Yep, a " << m[4]; // Careful - if the key does not exist, it will be created! }

OOP Etgar 2008 – Recitation 1019 Algorithms

OOP Etgar 2008 – Recitation 1020 How They Work The algorithms are global functions (and not methods of containers), defined in. Algorithms operate on ranges of elements: –E.g. we can sort only half of a container. –A range is specified by an iterator to its first element, and an iterator to one-past-last element (the range is half-open). –The caller must ensure that the range is valid. Not all containers work with all algorithms.

OOP Etgar 2008 – Recitation 1021 find InputIterator find (InputIterator begin, InputIterator end, const T& value); Finds the first occurrence of the element equal to value in the range, returns iterator to it. InputIterator find_if (InputIterator begin, InputIterator end, UnaryPredicate op); Same as above for element for which the unary predicate op (function or function object with one argument and bool return type) returns true. Both return end, if no such element is found. Complexity: linear.

OOP Etgar 2008 – Recitation 1022 max_element / min_element InputIterator max_element / min_element (InputIterator begin, InputIterator end); InputIterator max_element / min_element (InputIterator begin, InputIterator end, Compare comp); Return iterator to max/min element in the range. First version uses operator<() to compare, second uses comp(). Complexity: linear.

OOP Etgar 2008 – Recitation 1023 sort / stable_sort void sort / stable_sort (RandomAccessIterator begin, RandomAccessIterator end); void sort / stable_sort (RandomAccessIterator begin, RandomAccessIterator end, BinaryPredicate op); Sort all elements in the range using operator<() or op(). stable_sort() guarantees the order of equal elements remains the same. Cannot be used on list s (no random iterators). Complexity: n*log(n) on average.

OOP Etgar 2008 – Recitation 1024 for_each UnaryOperation for_each (InputIterator begin, InputIterator end, UnaryOperation op); Applies the unary function op to each element of the range. Returns a copy of op. Return value of op is ignored. –If op needs to modify its argument, it must be passed by reference. Complexity: linear.

OOP Etgar 2008 – Recitation 1025 Algorithms Example #include using namespace std; void print(int i) { cout << i << ' '; } struct MoreThan1 { bool operator()(int i) { return i > 1; } }; int main() { int a[5] = {5, 4, 1, 3, 2}; sort(&a[0], &a[5]);// Note a[5] - one-past-last element for_each(&a[0], &a[5], print); cout << '\n' << *( find_if(&a[2], &a[5], MoreThan1() ) ); return 0; }