SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Lists: An internal look
Chapter 6 Queues and Deques.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Data Structures Using C++ 2E
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
M180: Data Structures & Algorithms in Java
Beginning C++ Through Game Programming, Second Edition
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Basic C++ Sequential Container Features
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
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.
Data Structures Using C++ 2E
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
C++ STL CSCI 3110.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Lecture 7 : Intro. to STL (Standard Template Library)
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Intro to the C++ STL Timmie Smith September 6, 2001.
The Standard Template Library Container Classes Version 1.0.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
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.
1 The Standard Template Library Drozdek Section 3.7.
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.
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.
Object-Oriented Programming (OOP) Lecture No. 41
Lecture 04: Sequences structures
Programming with ANSI C ++
Standard Template Library
Standard Template Library (STL)
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Standard Template Library Model
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Standard Template Library
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:

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library

SEG Topic J - C++ Standard Template Library2 Standard Template Library (STL) The Standard Template Library defines powerful, template- based, reusable components -That implements common data structures and algorithms STL extensively uses generic programming based on templates Divided into three components: -Containers: data structures that store objects of any type -Iterators: used to manipulate container elements -Algorithms: searching, sorting and many others

SEG Topic J - C++ Standard Template Library3 Containers Three types of containers -Sequence containers: -linear data structures such as vectors and linked lists -Associative containers: -non-linear containers such as hash tables -Container adapters: -constrained sequence containers such as stacks and queues Sequence and associative containers are also called first- class containers

SEG Topic J - C++ Standard Template Library4 Iterators Iterators are pointers to elements of first-class containers Type const_iterator defines an iterator to a container element that cannot be modified Type iterator defines an iterator to a container element that can be modified All first-class containers provide the members functions begin() and end() return iterators pointing to the first and last element of the container

SEG Topic J - C++ Standard Template Library5 Iterators (cont.) If the iterator it points to a particular element, then -it++ (or ++it) points to the next element and -*it refers to the value of the element pointed to by it The iterator resulting from end() can only be used to detect whether the iterator has reached the end of the container We will see how to use begin() and end() in the next slides

SEG Topic J - C++ Standard Template Library6 Sequence Containers STL provides three sequence containers - vector: based on arrays - deque (double-ended queue): based on arrays - list: based on linked lists

SEG Topic J - C++ Standard Template Library7 Sequence Containers: vector The implementation of a vector is based on arrays Vectors allow direct access to any element via indexes Insertion at the end is normally efficient. The vector simply grows Insertion and deletion in the middle is expensive An entire portion of the vector needs to be moved

SEG Topic J - C++ Standard Template Library8 Sequence Containers: vector (cont.) When the vector capacity is reached then -A larger vector is allocated, -The elements of the previous vector are copied and -The old vector is deallocated To use vectors, we need to include the header Some functions of the class vector include -size, capacity, insert…

SEG Topic J - C++ Standard Template Library9 Example of using the class vector #include #include //vector class-template using std; int main() { vector v; // add integers at the end of the vector v.push_back( 2 ); v.push_back( 3 ); v.push_back( 4 ); cout << "\nThe size of v is: " << v.size() << "\nThe capacity of v is: " << v.capacity(); // display the content of v vector ::const_iterator it; for (it = v.begin(); it != v.end(); it++) { cout << *it << ‘ \n ’ ; } return 0; }

SEG Topic J - C++ Standard Template Library10 Sequence Containers: list list is implemented using a doubly-linked list Insertions and deletions are very efficient at any point of the list But you have to have access to an element in the middle of the list first bidirectional iterators are used to traverse the container in both directions Include header when using lists Some functions of the class list -push_front, pop_front, remove, unique, merge, reverse and sort

SEG Topic J - C++ Standard Template Library11 Sequence Containers: deque deque stands for double-ended queue deque combines the benefits of vector and list It provides indexed access using indexes (which is not possible using lists) It also provides efficient insertion and deletion in the front (which is not efficient using vectors) and the end

SEG Topic J - C++ Standard Template Library12 deque (cont.) Additional storage for a deque is allocated using blocks of memory -that are maintained as an array of pointers to those blocks Same basic functions as vector, in addition to that -deque supports push_front and pop_front for insertion and deletion at beginning of deque

SEG Topic J - C++ Standard Template Library13 Associative Containers Associative containers use keys to store and retrieve elements There are four types: multiset, set, multimap and map -all associative containers maintain keys in sorted order -all associative containers support bidirectional iterators -set does not allow duplicate keys -multiset and multimap allow duplicate keys -multimap and map allow keys and values to be mapped

SEG Topic J - C++ Standard Template Library14 Associative Containers: multiset Multisets are implemented using a red-black binary search tree for fast storage and retrieval of keys Multisets allow duplicate keys The ordering of the keys is determined by the STL comparator function object less Keys sorted with less must support comparison using the < operator

SEG Topic J - C++ Standard Template Library15 Example of using a multiset #include using std; int main() { multiset > ms; ms.insert( 10 ); // insert 10 ms.insert( 35 ); // insert 35 ms.insert( 10 ); // insert 10 again (allowed) cout << “ There are " << ms.count( 10 ); // returns the number of entries = 10 multiset >::iterator it; // creates an iterator it = ms.find(10); if ( it != ms.end() ) // iterator not at end cout << “ \n10 was found"; return 0; }

SEG Topic J - C++ Standard Template Library16 Associative Containers: set Sets are identical to multisets except that they do not allow duplicate keys To use sets, we need to include the header file

SEG Topic J - C++ Standard Template Library17 Associative Containers: multimap Multimaps associate keys to values They are implemented using red-black binary search trees for fast storage and retrieval of keys and values Insertion is done using objects of the class pair (with a key and value) Multimaps allow duplicate keys (many values can map to a single key) The ordering of the keys is determined by the STL comparator function object less

SEG Topic J - C++ Standard Template Library18 Example of using a multimap #include using std; typedef multimap > mp_type; // creates a mutlimap type int main() { mp_type mp; // value_type is overloaded in multimap to create objects of the class pair mp.insert( mp_type::value_type( 10, 14.5 ) ); mp.insert( mp_type::value_type( 10, 18.5 ) ); //allowed cout << "There are " << mp.count( 15 ) << "\n"; // use iterator to go through mp for (mp_type::iterator it = mp.begin(); it != mp.end(); it ++) cout first second << '\n'; return 0; }

SEG Topic J - C++ Standard Template Library19 Associative Containers: map They are implemented using red-black binary search trees just like multimaps Unlike multimaps, they allow storage and retrieval of unique key/value pairs. They do not allow duplicates of keys The class map overloads the [ ] operator to access values in a flexible way

SEG Topic J - C++ Standard Template Library20 Example of using a map The following code fragment shows how to use indexes with an object of the class map map > map_obj; // sets the value of key 20 to If subscript // 20 is not in map then creates new // key=20, value= pair map_obj [20] = ;

SEG Topic J - C++ Standard Template Library21 Container Adapters STL supports three container adapters: -stack, queue and priority_queue They are implemented using the containers seen before -They do not provide actual data structure Container adapters do not support iterators The functions push and pop are common to all container adapters

SEG Topic J - C++ Standard Template Library22 Container Adapters: stack Last-in-first-out data structure They are implemented with vector, list, and deque (by default) Header file Example of creating stacks -A stack of int using a vector:stack > s1; -A stack of int using a list: stack > s2; -A stack of int using a deque:stack s3;

SEG Topic J - C++ Standard Template Library23 Container Adapters: queue First-in-first-out data structure Implemented with list and deque (by default) Header file Example: -A queue of int using a list: queue > q1; -A queue of int using a deque:queue q2;

SEG Topic J - C++ Standard Template Library24 Container Adapters: priority_queue Insertions are done in a sorted order Deletions from front similar to a queue They are implemented with vector (by default) or deque The elements with the highest priority are removed first -less is used by default for comparing elements Header file

SEG Topic J - C++ Standard Template Library25 Algorithms STL separates containers and algorithms -The main benefit is to avoid virtual function calls -This cannot be done in Java or C# because they do not have such flexible mechanisms for dealing with function objects -Smalltalk does … all the following can be easily implemented in Smalltalk The subsequent slides describe most common STL algorithms

SEG Topic J - C++ Standard Template Library26 Fill and Generate fill(iterator1, iterator2, value); fills the values of the elements between iterator1 and iterator2 with value fill_n(iterator1, n, value); changes specified number of elements starting at iterator1 to value generate(iterator1, iterator2, function); similar to fill except that it calls a function to return value generate_n(iterator1, n, function) same as fill_n except that it calls a function to return value

SEG Topic J - C++ Standard Template Library27 Comparing sequences of values bool equal(iterator1, iterator2, iterator3); -compares sequence from iterator1 to iterator2 with the sequence beginning at iterator3 -return true is they are equal, false otherwise pair mismatch(iterator1, iterator2, iterator3); -compares sequence from iterator1 to iterator2 with the sequence starting at iterator3 -returns a pair object with iterators pointing to where mismatch occurred -example of the a pair object pair ::iterator, ::iterator> par_obj;

SEG Topic J - C++ Standard Template Library28 Removing elements from containers iterator remove(iterator1, iterator2, value); -removes all instances of value in a range iterator1 to iterator2 -does not physically remove the elements from the sequence -moves the elements that are not removed forward -returns an iterator that points to the "new" end of container iterator remove_copy(iterator1, iterator2, iterator3, value); -copies elements of the range iterator1-iterator2 that are not equal to value into the sequence starting at iterator3 -returns an iterator that points to the last element of the sequence starting at iterator3

SEG Topic J - C++ Standard Template Library29 Replacing elements (1) replace( iterator1, iterator2, value, newvalue ); replaces value with newvalue for the elements located in the range iterator1 to iterator2 replace_if( iterator1, iterator2, function, newvalue ); replaces all elements in the range iterator1-iterator2 for which function returns true with newvalue

SEG Topic J - C++ Standard Template Library30 Replacing elements (2) replace_copy( iterator1, iterator2, iterator3, value, newvalue ); replaces and copies elements of the range iterator1-iterator2 to iterator3 replace_copy_if( iterator1, iterator2, iterator3, function, newvalue ); replaces and copies elements for which function returns true where iterator3

SEG Topic J - C++ Standard Template Library31 Search algorithms iterator find(iterator1, iterator2, value) returns an iterator that points to first occurrence of value iterator find_if(iterator1, iterator2, function) returns an iterator that points to the first element for which function returns true.

SEG Topic J - C++ Standard Template Library32 Sorting algorithms sort(iterator1, iterator2) sorts elements in ascending order binary_search(iterator1, iterator2, value) searches in an ascending sorted list for value using a binary search

SEG Topic J - C++ Standard Template Library33 Copy and Merge copy(iterator1, iterator2, iterator3) copies the range of elements from iterator1 to iterator2 into iterator3 copy_backward(iterator1, iterator2, iterator3) copies in reverse order the range of elements from iterator1 to iterator2 into iterator3 merge(iter1, iter2, iter3, iter4, iter5) ranges iter1-iter2 and iter3-iter4 must be sorted in ascending order and copies both lists into iter5 in ascending order

SEG Topic J - C++ Standard Template Library34 Unique and Reverse order iterator unique(iterator1, iterator2) -removes (logically) duplicate elements from a sorted list -returns iterator to the new end of sequence reverse(iterator1, iterator2) -reverses elements in the range of iterator1 to iterator2

SEG Topic J - C++ Standard Template Library35 Utility algorithms (1) random_shuffle(iterator1, iterator2) randomly mixes elements in the range iterator1-iterator2 int count(iterator1, iterator2, value) returns number of instances of value in the range int count_if(iterator1, iterator2, function) counts number of instances for which function returns true

SEG Topic J - C++ Standard Template Library36 Utility algorithms (2) iterator min_element(iterator1, iterator2) returns iterator to smallest element iterator max_element(iterator1, iterator2) returns iterator to largest element accumulate(iterator1, iterator2) returns the sum of the elements in the range

SEG Topic J - C++ Standard Template Library37 Utility algorithms (3) for_each(iterator1, iterator2, function) calls function on every element in range transform(iterator1, iterator2, iterator3, function) calls function for all elements in range iterator1-iterator2, and copies result to iterator3

SEG Topic J - C++ Standard Template Library38 Algorithms on sets (1) includes(iter1, iter2, iter3, iter4) returns true if iter1-iter2 contains iter3-iter4. Both sequences must be sorted set_difference(iter1, iter2, iter3, iter4,iter5) copies elements in range iter1-iter2 that do not exist in second range iter3-iter4 into iter5 set_intersection(iter1, iter2, iter3, iter4, iter5) copies common elements from the two ranges iter1-iter2 and iter3-iter4 into iter5

SEG Topic J - C++ Standard Template Library39 Algorithms on sets (2) set_symmetric_difference(iter1, iter2, iter3, iter4, iter5) copies elements in range iter1-iter2 that are not in range iter3-iter4 and vice versa, into iter5. Both sets must be sorted set_union(iter1, iter2, iter3, iter4, iter5) copies elements in both ranges to iter5. Both sets must be sorted

SEG Topic J - C++ Standard Template Library40 References "C++, How to program", Harvey M. Deitel, Paul J. Deitel, 4th edition, Prentice Hall "The C++ Programming Language", Bjarne Stroustrup, 3rd Edition, Addison-Wesley