CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Advertisements

Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
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,
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
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.
Chapter 19 Java Data Structures
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Templates and the STL.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Data Structures Using C++ 2E
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
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.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Collision Resolution Open Addressing Open Addressing Separate Chaining.
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.
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)
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.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
1 Introduction to Hashing - Hash Functions Sections 5.1, 5.2, and 5.6.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
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.
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:
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:
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
C++ Review STL CONTAINERS.
Associative Containers Sets Maps Section 4.8. Associative Containers.
Hashing. Search Given: Distinct keys k 1, k 2, …, k n and collection T of n records of the form (k 1, I 1 ), (k 2, I 2 ), …, (k n, I n ) where I j is.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Collection types CS Chakrabarti Motivation  Thus far the only collection types we have used are vector and matrix  Problem #1: given an input.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
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.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
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.
Design of a HashTable and its Iterators
Design of a HashTable and its Iterators
Advanced Associative Structures
Associative Structures
CS212: Object Oriented Analysis and Design
Recitation Outline C++ STL associative containers Examples
Iterators and STL Containers
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
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
Implementing the Associative Containers
ENERGY 211 / CME 211 Lecture 20 November 5, 2008.
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type of keys the container holds –A value_type gives the type of values the container holds –A mapped_type gives type associated with key (maps only) Examples for sets (key and value types are the same) –Associated type set ::key_type is int, as is set ::value_type Examples for maps (note key becomes const in value) –Associated type map ::key_type is int –Type map ::mapped_type is string –Associated type map ::value_type is pair

CSE 332: C++ Associative Containers II Iteration Through Associative Containers Iteration is bi-directional –Can increment (e.g., ++iter1 ) or decrement (e.g., --iter1 ) an iterator Dereferencing gives read-only access to keys –E.g., *iter1 = “D”; or iter2->first = “D”; are not allowed But, can gain read-write access to mapped value –E.g., iter2->second = 7; is allowed set multiset map multimap 2B 3A 5C 2C 7D 2B 3A 2C 7D B A C C D B A C D iter1 iter2

CSE 332: C++ Associative Containers II Adding Elements to a Map or Set Insert returns a pair (may also rebalance the tree) –First part is an iterator to element, second is bool ( true on success) –Multimap or multiset insert just returns an iterator to inserted element For maps, insert takes a pair (several possible ways) –E.g., m.insert({“C”, 5}); if compiler supports list initialization –E.g., m.insert(make_pair(“C”, 5)); –E.g., m.insert(pair (“C”, 5)); –E.g., m.insert(map ::value_type(“C”, 5)); set (before) set (after) map (before) 2B 3A 5C 2D 7E 2B 3A 2D 7E B C D E E map (after) B D

CSE 332: C++ Associative Containers II Finding Elements in a Multimap or Multiset Find returns an iterator to first matching element –Forward iteration from first matching element (if one was found) gives all other equal elements (find returns past the end iterator if no match) Can obtain iterators bounding range of equal elements –E.g., s.lower_bound(“B”); gives first element not less than “B” –E.g., s.upper_bound(“B”); gives first element greater than “B” –E.g., s.equal_range(“B”); returns a pair of iterators bounding the range of elements with key “B” (or insertion point if match not found) 2B 3A 5B 2C 7C B A B C C iter1 iter2 iter1 iter2

CSE 332: C++ Associative Containers II Unordered Containers (UCs) UCs use == to compare elements instead of < to order them –Types in unordered containers must be equality comparable –When you write your own structs, overload == as well as < UCs store elements in indexed buckets instead of in a tree –Useful for types that don’t have an obvious ordering relation over their values UCs use hash functions to put and find elements in buckets –May improve performance in some cases (if performance profiling suggests so) –Declare UCs with pluggable hash functions via callable objects, decltype, etc. A unordered_setunordered_multiset unordered_mapunordered_multimap 0BC ABC C A7B2C 0A7B2C 3C

CSE 332: C++ Associative Containers II Concluding Remarks Choose carefully which operators you use –E.g.,, m.insert(map ::value_type(“C”, 5)); avoids construct/destruct/assign of a temporary (vs. m[“C”] = 5); ) Also realize that overloaded operator[] has different interpretations in different containers –In a vector or other random access sequence container it’s a constant time indexing operation to a particular location –In a map or other associative container it’s a logarithmic time operation (“find” or “insert” versus “read” or “write”) Unordered containers give another mix of operations –E.g., via hashing, for which callable objects can be used