Associative Containers Sets Maps Section 4.8. Associative Containers.

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE Lecture 15 – Maps.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
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.
1 Associative Containers Gordon College Prof. Brinton.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
More on the STL vector list stack queue priority_queue.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Trees.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
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.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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
Data Structures.  Consider storing data for 100 employees by their Social Security Numbers (SSN)  SSN range: –  A fast search:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
Trees Basic concepts of trees Implementation of trees Traversals of trees Binary trees Binary search trees AVL trees B-trees.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)
Generic Programming Using the C++ Standard Template Library.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
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.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.
Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming.
The Utility Class pair All associative containers make use of the pair template, which is defined in. Slightly stripped down, it looks basically like this:
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
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.
The Standard Template Library Container Classes Version 1.0.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
A couple of slides on containers…
What remains Topics Assignments Final exam
Chapter 9 – Sets and Maps 9.1 Associative Container
Associative Structures
Containers & Iterators
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Multiset Class and its Iterator
Trees Basic concepts of trees Implementation of trees
Elements are always copied when they are put into a container
Lists - I The List ADT.
Lists - I The List ADT.
Generic Set Algorithms
Chapter 3 Lists, Stacks, and Queues
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Recitation Outline Hash tables in C++ STL Examples Recursive example
The List Container and Iterators
A dictionary lookup mechanism
Presentation transcript:

Associative Containers Sets Maps Section 4.8

Associative Containers

Generic Associative Containers A container that is organized and accessible by value  Client may insert / remove any T-object in C  Client may not determine position in C Bidirectional Iterators  Both ++ and -- operations are supported

Multimodal / Unimodal Assoc. Containers Multimodal associative container  Duplicate elements are allowed  Insert operations always increase size by 1 Unimodal associative container  Duplicate elements not allowed  Insert operations have dual personality If t is not in C, then Insert(t) If t is in C, then overwrite the existing t

Generic Sorted Associative Containers A generic associative container Traverse elements in sorted order for (C::Iterator I = c.Begin(); I != c.End(); ++I){ cout << *I; } May be either unimodal or multimodal

The Pair Template Some background

The Pair Template A class that holds a pair of items template class Pair { public: T first; T second; Pair(); Pair(T t1, T t2); };

The Pair Template (contd.) Constructors template Pair ::Pair() { } template Pair ::Pair(T t1, T t2) : first(t1), second(t2) { }

The Pair Template (contd.) Declarations Pair intPair1(1, 2); Pair intPair2(3, 4); Pair floatPair(1.1, 2.2); Pair > anotherPair(intPair1, intPair2);

The Pair Template (contd.) A class that holds a pair of items with different types template class Pair { public: T1 first; T2 second; Pair(); Pair(T1 t1, T2 t2); };

The Pair Template (contd.) Constructors template Pair ::Pair() { } template Pair ::Pair(T1 t1, T2 t2) : first(t1), second(t2) {}

Sets & Maps

Sets Associative containers Set  A sorted associative container that does not allow duplicates  Stores objects  Unimodal: duplicate objects not allowed MultiSet  A sorted associative container that allows duplicates  Stores objects  Multimodal: duplicate objects OK  Also known as Bags

The STL Set Template set() // Creates an empty set. set(const key_compare& comp) // Creates an empty set, use comp // for key comparison set(iterator f, iterator l) // Creates a set with a copy of a range set(iterator f, iterator l, const key_compare& comp) // Creates a set with a copy of a range, // using comp as the key_compare object pair insert(const value_type& x) iterator insert(iterator pos, const value_type& x) // Inserts x into the set, using pos as a hint to where it will be inserted void insert(iterator I1, iterator I2) // Inserts a range into the set

void erase(iterator pos) // Erases the element pointed to by pos. size_type erase(const key_type& k) // Erases the element whose key is k. void erase(iterator first, iterator last) // Erases all elements in a range. iterator find(const key_type& k) const // Finds an element whose key is k. Logarithmic complexity for insertion, remove, search

Example Set Clients Inventory struct StockItem { // barcode, name, amount }; void print_inventory(std::ostream&os, const set & inventory) { set ::iterator I; for (I = inventory.begin(); I != inventory.end(); ++I) { os << *I; } Customer accounts class Customer { // ssn, account_number, last_name, first_name… }; int main() { set customers; }

Maps Associative container that associates objects of type Key with objects of type Data Map  Stores (key, object) pairs  Unimodal on keys: duplicate keys not allowed  AKA: table, associative array MultiMap  Stores (key, object) pairs  Multimodal: duplicate keys OK

The STL Map Template map() map(const key_compare& comp) map(iterator f, iterator l) map(iterator f, iterator l, const key_compare& comp) pair insert(const value_type& x) // Inserts x into the map iterator insert(iterator pos, const value_type& x) // Inserts x into the map, using pos as hint to where it will be inserted void insert(iterator, iterator) // Inserts a range into the map

STL Map (continued) void erase(iterator pos) // Erases the element pointed to by pos size_type erase(const key_type& k) // Erases the element whose key is k. void erase(iterator first, iterator last) // Erases all elements in a range. iterator find(const key_type& k) // Finds an element whose key is k. data_type& operator[](const key_type& k)  Returns a reference to the object that is associated with a particular key.  If the map does not already contain such an object, operator[] inserts the default object data_type()

Map Usage Example struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; } }; int main() { map months; months["january"] = 31; months["february"] = 28; months["march"] = 31; months["april"] = 30; months["may"] = 31; months["june"] = 30; months["july"] = 31; months["august"] = 31; months["september"] = 30; months["october"] = 31; months["november"] = 30; months["december"] = 31; cout " << months["june"] << endl; map ::iterator cur = months.find("june"); map ::iterator prev = cur; map ::iterator next = cur; ++next; --prev; cout << "Previous (in alphabetical order) is " << (*prev).first << endl; cout << "Next (in alphabetical order) is " << (*next).first << endl; }