1 159.234LECTURE 17 159.234 LECTURE 17 More on the Standard Template Library 23 A guided tour with common examples of the containers.

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Introduction to the STL
The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
1 Associative Containers Gordon College Prof. Brinton.
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.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
Quick Overview of STL STL = Standard Template Library Main concept : Container, Iterator Application : Linked list, Stack etc.
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.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures Using C++ 2E
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.
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.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms Slide
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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)
C++ STL CSCI 3110.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
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,
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 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.
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.
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.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
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
Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms.
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
priority_queue<T>
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library
Standard Template Library
Assign #3: Enhance you Tank Game Assign #3: Graphical Menu System
A dictionary lookup mechanism
Presentation transcript:

LECTURE LECTURE 17 More on the Standard Template Library 23 A guided tour with common examples of the containers

2 STL has: bitseta set of bits dequea double ended-queue lista linear list mapkey value pairs (unique association) multimapkey-value pairs (multiple associations) multisetset (elements need not be unique) priority_queuea priority based queue queuea queue seta set in which elements are unique stacka stack vectora dynamic array More on STL

3 #include using namespace std; int main(){ vector v; vector ::iterator it, start, end; for(int i=0;i<10;i++){ v.push_back(i); } cout << "v size = " << v.size() << endl; start = v.begin(); start += 3; // now points at element [3] end = start; end += 4; // now points at element [7] v.erase(start, end ); // erases 3,4,5,6 cout << "v size = " << v.size() << endl; for(it=v.begin();it < v.end();it++){ *it *= 10; } cout << "v contains: "; for(int i=0;i<v.size(); i++){ cout << " " << v[i]; } cout << endl; } Output: % a.out v size = 10 v size = 6 v contains: vector v; A variable-sized vector the range includes all the elements between start and end, including the element pointed by start but not the one pointed by end.

4 list l1, l2; l1.push_front(2);l1.push_front(4);l1.push_front(6);l1.push_front(8); l2.push_back(1);l2.push_back(3);l2.push_back(5); l2.push_back(7); for(list ::iterator it = l1.begin(); it != l1.end(); it++){ cout << *it << " "; } cout << endl; l1.sort(); for(list ::iterator it = l1.begin(); it != l1.end(); it++){ cout << *it << " "; } cout << endl; for(list ::iterator it = l2.begin(); it != l2.end(); it++){ cout << *it << " "; } cout << endl; l1.merge( l2 ); for(list ::iterator it = l1.begin(); it != l1.end(); it++){ cout << *it << " "; } cout << endl; Output: % a.out list list Doubly-linked list efficient insertion and deletion Allows efficient insertion and deletion at any position in the sequence. Has all functions of deque, except operator[], and at() functions Has extra functions: splice, remove, unique, merge, reverse, sort Merges l2 into the l1 list, inserting all the elements of l2 into l1 at their respective ordered positions. This empties l2 and increases the l1 list size.

5 map m1, m2; map ::iterator iter; // points to a pair string one("One"); string two("Two"); string three("Three"); string four("Four"); string five("Five"); m1[one] = 1; m2[two] = 2; m1[three] = 3; m2[four] = 4; m1[five] = 5; for(iter=m1.begin();iter!=m1.end();iter++){ cout second first << endl; } cout << endl; m1.swap(m2); for(iter=m1.begin();iter!=m1.end();iter++){ cout second first << endl; } Note: map’s iterator is a pointer to a pair - dereferenced using members first and second Output: % a.out Found 5 keyed by Five Found 1 keyed by One Found 3 keyed by Three Found 4 keyed by Four Found 2 keyed by Two map map An associative array (or dictionary, or table) acts like an array whose index can be any type that implements the < operator. e.g. key=StudentID, val = Student’s data Record

6 multimap m1; multimap ::iterator iter; // points to a pair string one("One"); string two("Two"); string three("Three"); string four("Four");string five("Five");string six("Six"); m1.insert( m1.end(), make_pair(one, 1) ); // cannot use [] notation m1.insert( m1.end(), make_pair(two, 2) ); m1.insert( m1.end(), make_pair(three, 3) ); m1.insert( m1.end(), make_pair(four, 4) ); m1.insert( m1.end(), make_pair(five, 5) ); m1.insert( m1.begin(), make_pair(six, 6) ); m1.insert( m1.begin(), make_pair(four, 40) ); for(iter=m1.begin();iter!=m1.end();iter++){ cout second first << endl; } cout << endl; NB #include, not ! Output: % a.out Found 5 keyed by Five Found 4 keyed by Four Found 40 keyed by Four Found 1 keyed by One Found 6 keyed by Six Found 3 keyed by Three Found 2 keyed by Two multimap multimap Map in which a key can occur many times

7 #include using namespace std; int main(){ stack s; // has no iterator cout << "Pushing onto stack: "; for(int i=0; i < 6 ;i++){ cout << " " << i; s.push(i); } for(int i=12; i >= 6 ;i--){ cout << " " << i; s.push(i); } cout << endl; cout << "Stack size " << s.size() << endl; cout << " Top of stack is:"; while( !s.empty() ){ cout << " " << s.top(); s.pop(); // discards top of stack } cout << endl; cout << "Stack size " << s.size() << endl; } Output: % a.out Pushing onto stack: Stack size 13 Top of stack is: Stack size 0 stack<T> A stack Allows insertions and deletions only at one end, called its top adapted from the deque class template No Iterator

8 #include using namespace std; int main(){ queue q; // has no iterator for(int i=0; i < 6 ;i++){ q.push(i); } cout << "Queue size " << q.size() << endl; cout << "Queue empty ? " << boolalpha << q.empty() << endl; cout << "Queue contains:"; while( !q.empty() ){ cout << " " << q.front(); q.pop(); // discards head of queue } cout << endl; cout << "Queue size " << q.size() << endl; } Output: % a.out Queue size 6 Queue empty ? false Queue contains: Queue size 0 queue q; A queue, has no iterator, allows duplicate elements Allows insertions only at one end, called its back (or rear), and deletions only at the other end adapted from the deque class template No iterator

9 #include using namespace std; int main(){ // priority_queue q; // has no iterator priority_queue, less > q; // by default vector is used as container // by default less is used as comparator for(int i=0; i < 6 ;i++){ q.push(i); } cout << "Queue size " << q.size() << endl; cout << "Queue empty ? " << boolalpha << q.empty() << endl; cout << "Priority element is " << q.top() << endl; cout << "Queue priority elements, in order, are:"; while( !q.empty() ){ cout << " " << q.top(); q.pop(); // discards head of queue } cout << endl; cout << "Queue size " << q.size() << endl; } NB #include, not priority_queue less Output (with less): % a.out Queue size 6 Queue empty ? false Priority element is Queue priority elements, in order, are: Queue size 0 greater Output (with greater): % a.out Queue size 6 Queue empty ? false Priority element is Queue priority elements, in order, are: Queue size 0 priority_queue<T> A queue sorted by value, allows duplicate elements,has no iterator the top element of the sequence compares largest, or highest priority highest priority element is maintained at the top (or front)

10 set s1, s2, s3; set ::iterator it; for(int i=9;i>=0;i--){ if( i%2 ) s1.insert(s1.begin(), i); else s2.insert(s2.end(), i); } cout << "size of s1 is " << s1.size() << endl; cout << "size of s2 is " << s2.size() << endl; cout << "s1 contains"; for(it=s1.begin();it != s1.end(); it++){ cout << " " << *it; } cout << endl; cout << "s2 contains"; for(it=s2.begin();it != s2.end(); it++){ cout << " " << *it; } cout << endl; s3 = s1; s3.insert( s2.begin(), s2.end() ); cout << "s3 contains"; for(it=s3.begin();it != s3.end(); it++){ cout << " " << *it; } cout << endl; if( s3.find(4) != s3.end() ) cout << "s3 contains 4" << endl; else cout << "s3 does not contain 4" << endl; if( s3.find(11) != s3.end() ) cout << "s3 contains 11" << endl; else cout << "s3 does not contain 11" << endl; Output: % a.out size of s1 is 5 size of s2 is 5 s1 contains s2 contains s3 contains s3 contains 4 s3 does not contain 11 set set A set like a map with only the unique keys stored Internally, the elements in a set are always sorted from lower to higher following a specific strict weak ordering criterion set on container construction. The find() function returns an iterator that designates the earliest element in the controlled sequence whose sort key equals key. If no such element exists, the iterator equals end().

11 multiset ms; multiset ::iterator iter; // points to a pair ms.insert( ms.end(), 1); // cannot use [] notation ms.insert( ms.end(), 3); ms.insert( ms.end(), 2); ms.insert( ms.end(), 4); ms.insert( ms.end(), 5); cout << "Multiset contains: "; for(iter=ms.begin();iter!=ms.end();iter++){ cout << " " << *iter; } cout << endl; iter = ms.find(3); cout << "Found " << *iter++ << " followed by " << *iter << endl; iter = ms.find(4); cout << "Found " << *iter++ << " followed by " << *iter << endl; ms.erase(iter); cout << "Multiset now contains: "; for(iter=ms.begin();iter!=ms.end();iter++){ cout << " " << *iter; } cout << endl; #include NB #include, not multiset! Output: % a.out Multiset contains: Found 3 followed by 4 Found 4 followed by 4 Multiset now contains: multiset<T> A set in which a value can occur many times

12 #include using namespace std; int main(){ const int N = 24; bitset bs; bs.reset(); bs.set(1); bs.set(2); bs.set(4); bs.set(8); cout << bs[8] << endl; for(int i = N-1; I >= 0; i--){ cout << bs.test(i); if (! ( i%8 ) ) cout << " "; } cout << endl; bs <<= 2 ; for(int i=N-1;i>=0;i--){ cout << bs.test(i); if (! ( i%8 ) ) cout << " "; } cout << endl;} } Output is: % a.out bitset<T> stores a sequence of N bits. stores a sequence of N bits. set 1reset 0 A bit is set if its value is 1, reset if its value is 0. To flip a bit is to change its value from 1 to 0 or from 0 to 1. When converting between an object of class bitset and an object of some integral type, bit position j corresponds to the bit value 1 << j. The integral value corresponding to two or more bits is the sum of their bit values. see Bitset.cpp

13 #include using namespace std; int main(){ const int N = 24; bitset bs; bs.reset(); bs.set(1); bs.set(2); bs.set(4); bs.set(8); cout << bs[8] << endl; for(int i=N-1;i>=0;i--){ cout << bs.test(i); if (! ( i%8 ) ) cout << " "; } cout << endl; bs <<= 2 ; for(int i=N-1;i>=0;i--){ cout << bs.test(i); if (! ( i%8 ) ) cout << " "; } cout << endl;} } Output is: % a.out bitset<T> stores a sequence of N bits. bool test(size_t pos, bool val = true); if pos >= size(), throws out_of_range. Otherwise, it returns true only if the bit at position pos is set. bitset & reset(); resets all bits in the bit sequence, then returns *this. bitset & set(size_t pos, bool val = true); if pos >= size(), throws out_of_range. Otherwise, it stores val in the bit at position pos, then returns *this.

14 // enumeration type for the bits; each bit represents a color enum Color { red, yellow, green, blue, white, black, numColors }; bitset usedColors; // create bitset for all bits/colors // set bits for two colors usedColors.set(red); usedColors.set(blue);bitset<T> Another example: bitset operator & pos); member operator function returns bitset(*this) <<= pos. size_t count() const; member function returns the number of bits set in the bit sequence. bitset operator~(); member operator function returns bitset(*this).flip(). See also Bitset - Colour.cpp See also Bitset - integral to binary.cpp

15 deque dq; dq.push_back( 4 ); dq.push_back( 5 ); dq.push_front( 3 ); dq.push_front( 2 ); dq.push_front( 1 ); cout << "Max size of deque is " << dq.max_size() << endl; cout << "Element 2 is " << dq[2] << endl; for(deque ::iterator it=dq.begin();it!=dq.end();it++){ cout << *it << " "; } cout << endl; deque ::iterator it=dq.begin(); it += 3; dq.erase( it ); for(deque ::reverse_iterator rit=dq.rbegin();rit!=dq.rend();rit++){ cout << *rit << " "; } cout << endl; deque dq2; dq2.push_front( 30 ); dq2.push_front( 20 ); dq2.push_front( 10 ); dq.swap( dq2 ); for(deque ::iterator it=dq.begin();it!=dq.end();it++){ cout << *it << " "; } cout << endl; Output: % a.out Max size of deque is 4,294,967,295 Element 2 is deque<T> Double-ended queue efficient insertion and deletion Allows efficient insertion and deletion at both its beginning and its end Has all functions of vector, except capacity() and reserve() functions Extra functions: push_front(), pop_front()

16 STL provides useful containers Can be parameterised by a contained class iterators useful Not all have iterators See “The Complete Reference C++” Fourth Edition, Herbert Schildt, McGraw Hill, ISBN , Chaper 24 and Chapter 33. STL Container Summary

17 Assign #3: Enhance your Tank Game Optional, in effect cancel the lowest assignment Provided that you submitted at least 3 assignments Assign #3: Graphical Menu System or

18 Add another instance of your Tank class; incorporate random facilities that would make that Tank move, jump (at Ledges) and shoot at the Alien at different angles, velocity and speed at random time intervals Requirements: –Multiple bullets should be released by each Tank object –Tank nozzle should be allowed to point at different angles Continue working on your Tank game –Improve class architecture to allow for multiple bullets Add another Tank as support Tank Assign #3: Enhance you Tank Game Optional, in effect cancel the lowest assignment Provided that you submitted at least 3 assignments

19 Graphical menu system –Pull-down menu should appear once a menu item is clicked, and retract whenever necessary –Simply report on screen which item has been clicked Requirements: –Use STL for the core of your menu system –Use simple rectangles for the menu items, with labels on them –You should provide a documentation for the structure of the file your menu system is able to accept –You should also provide a documentation of your system’s class design, as well as all the functions that you incorporate –Additional mark for Graphic enhancements Load menu items from a text file Define your own file structure Mouse-clickable menu items and sub menu items Assign #3: Graphical Menu System Optional, in effect cancel the lowest assignment Provided that you submitted at least 3 assignments