Lecture 11 Standard Template Library Lists Iterators Sets Maps.

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.
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.
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.
Lecture 20 “Standard” Template Library. What is the Standard Template Library? A collection of C++ classes (templates) containers vectors lists stacks.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
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
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.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
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.
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.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
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.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
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.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact) l they serve the same purpose as arrays but using.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Standard Template Library a collection of useful tools.
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.
Standard Template Library
Introduction to olympic programming
Standard Template Library (STL)
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 9 One-Dimensional Arrays
Recursive Linked List Operations
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
C++ STL Stack, Queue, and Deque
Lab4 problems More about templates Some STL
the Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

Lecture 11 Standard Template Library Lists Iterators Sets Maps

Lists #include Doubly-linked list Allowing insertion, deletion and modification of elements at the front, the back and in the middle size, push_back, push_front, pop_back and pop_front. Mechanism of iterators is needed

Iterators Vectors, lists, queues, etc. are all examples of collections. In fact, they are all subclasses of a Container class in STL An iterator is like a reference (or pointer) to one of the elements of a collection. Supports at least two operations: 1) ++ operator: advance forward one position 2) unary * operator : return the element being referenced

Lists & Iterators: Example 1. list ::iterator: class name for an iterator over a list of integers. 2. i: iterator, initialized to c.begin() list c;//Creates an empty list of integers c.push_back( 3 ); c.push_back( 5 ); c.push_front( 6 ); //Add 3 elements to the list, 6, 3, 5 for( list ::iterator i = c.begin(); i != c.end(); ++i ) { cout << " " << *i; }

Lists & Iterators: Example list c;//Creates an empty list of integers c.push_back( 3 ); c.push_back( 5 ); c.push_front( 6 ); //Add 3 elements to the list, 6, 5, 3 for( list ::iterator i = c.begin(); i != c.end(); ++i ) { cout << " " << *i; } 3. The begin() method of every collection returns an iterator pointing to the first element of the collection. 4. The end() method returns an iterator pointing to the position in the collection that is one after the last element. In other words, when i is equal to c.end(), i has run off the end of the list and is pointing to a nonexistent position – the loop should then stop. 5. *i – the value that i is referencing. Result: _6_3_5

More Example vector v( 10, 7.0 ); //creates a vector of 10 elements, each is 7 list w( v.begin(), v.end() ); //creates a list containing copy vector x( ++w.begin(), w.end() ); //creates another vector, 9 elements 1. Same as using iterators with Java collections, but with a very different syntax. 2. An important difference is the pair of methods, begin() and end(). Think of them as defining an interval [begin, end) that spans the entire collection. The left endpoint is included in the range, and the right endpoint is excluded. 3. If begin() is equal to end()? 4. You can also pass a range to the constructor of most collections as follows.

Sets #include It is a sorted collection of elements, stored as a balanced binary search tree. No push_back or push_front, insert() instead.(sorted) To store elements of type C in a set, the < operator must be defined for type C. (Opertator Overloading) ( You can safely declare sets of int, double, char or any othe primitive types because you can use the < operator to compare two ints, two doubles or two chars – the operator is already defined in C++. )

Sets: Example 1. set ::iterator : class name for an iterator over a set of strings 2. j: iterator, initialized to ss.begin() 3. ss.begin(): smallest element; ss.end(): one after the last element of the set 4. ++j increments the iterator. *j returns the string being referenced by j. set ss; //creates an empty set of strings, #include ss.insert( "Sheridan" ); ss.insert( "Delenn" ); ss.insert( "Lennier" ); //add elements to the set for( set ::iterator j = ss.begin(); j != ss.end(); ++j ) { cout << " " << *j; } Result: _Delenn_Lennier_Sheridan

More Examples set s( v.begin(), v.end() ); //Create a set s by passing it a range containing all of the elements of v. Time: O(n*log(n)) vector w( s.begin(), s.end() ); //Create another vector, w, containing all of the elements of s. Time: Linear set s; for( int i = 1900; i <= 3000; i += 4 ) { s.insert( i ); } for( int i = 1900; i <= 3000; i += 100 ) { if( i % 400 != 0 ) { if( s.count( i ) == 1 ) s.erase( i ); } 1.Erase(): remove an element 2.Count(): check whether an element is in the set

Maps #include store key-value pairs rather than collection of elements. maps are sorted by the keys. Implemented using balanced binary search trees. insertion, deletion and update operations require logarithmic time in the size of the map. The map class has 2 template parameters – key type and value type. (using [] to access entries)

Maps: Example vector i2s( 7 ); i2s[0] = "Sunday"; i2s[1] = "Monday"; i2s[2] = "Tuesday"; i2s[3] = "Wednesday“; i2s[4] = "Thursday"; i2s[5] = "Friday"; i2s[6] = "Saturday"; map s2i; for( int i = 0; i < 7; i++ ) { s2i[i2s[i]] = i; }

Maps: More Example string word; map freq; while( cin >> word ) { freq[word]++; } for( map ::iterator i = freq.begin(); i != freq.end(); ++i ) { cout << (*i).first << ": " << (*i).second << endl; } //pair classes?

STL – Pair in Map the STL defines a class pair that simply stores 2 things. pair has 2 member variables (or fields): first is of type A and second is of type B. To create a pair, you need to manually specify what A and B are, like this. pair p( "pi", 3.14 ); //make_pair(“pi”, 3.14); p.second = ; The first line calls the constructor. To use pair, you need to #include. (map: like Java‘s TreeMap - basically, a set of key- value pairs, sorted by the key.) template class pair { public: A first; B second; pair( A a, B b ) { first = a; second = b; } }; int min( int a, int b ) { return a < b ? a : b; } (templates, chars, doubles, strings, and anything else that has the '<' operator defined on it. ) template C min( C a, C b ) { return a < b ? a : b; }

Maps: More Example map freq; vector commonwords; commonwords.push_back( "and" ); for ( int i=0; i < commonwords.size(); i++ ) { if ( freq.count( commonwords[i] ) ) // Check if this common word is in the map freq.erase( commonwords[i] ); // Erase it so our map don't count it } count(): check whether a given key exists in a map. erase(k): erase the key-value pair whose key is equal to k. size(): return the size of the map.