STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.

Slides:



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

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
1 Associative Containers Gordon College Prof. Brinton.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
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.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
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.
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.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
1 Tree Indexing (1) Linear index is poor for insertion/deletion. Tree index can efficiently support all desired operations: –Insert/delete –Multiple search.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
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:
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
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.
CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
C++ Review STL CONTAINERS.
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
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.
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.
Final Exam Review COP4530.
Standard Template Library (STL)
Hashing Exercises.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
What remains Topics Assignments Final exam
Advanced Associative Structures
Associative Structures
Final Exam Review COP4530.
Recitation Outline C++ STL associative containers Examples
STL Containers Some other containers in the STL.
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.
CO4301 – Advanced Games Development Week 12 Using Trees
Implementing the Associative Containers
Standard Template Library
Presentation transcript:

STL Associative Containers navigating by key

Pair Class aggregates values of two, possibly different, types used in associative containers defined in may use a two-argument constructor may copy and compare may access individual elements with first and second may create with std::make_pair(arg1,arg2) function that returns a pair composed of arg1 and arg2 2

Associative Containers do not store elements in linear order, provide mapping from keys to values usually insertion, deletion, lookup times are equivalent types ordered containers (preserve iterator order of elements) map – stores pairs, first element is key, lookup by key multimap – map with multiple elements with same key set – element and key are the same multiset – multiple elements allowed unordered containers (hashes) – C++11, do not order elements, quicker access unordered_map unordered_multimap unordered_set unordered_multiset 3

Maps stores sorted key/value pairs sorting is based on key insertion/deletion/lookup takes logarithmic time need and std::map declaring: map employees; inserting –“safe” with iterator auto ret=employees.insert(make_pair(123, ”Joe”)); returns pair ::iterator, bool> does not overwrite old pair with same key –second pair element indicates whether map element with key already exists –first pair element iterator to existing or inserted map element if(ret.second) cout << ”insert successful!” << endl; –“unsafe” with overloaded indexing operator, overwrites old key value if exists, creates new if does not employees[123] = ”Joe”; 4

Maps (cont.) accessing all elements –may iterate over map (in order of keys) to access elements for (auto it=employees.cbegin(); it!=employees.cend(); ++it) cout second << endl; –using range-based for (C++11) for(const auto& e: employees) cout << e.first << ": " << e.second << endl; looking up –with indexing employee[123] = ”Joe”; // inserts non-existent –with find, returns end() if not found auto it = employees.find(123); if (it != employees.end()) cout << “Found it!” erasing –may erase at iterator, or iterator range (same as with vectors) –may erase by key: employee.erase(123); const- maps and maps of constant elements are allowed key is always implicitly const 5

Maps Implementation usually implemented using red-black tree variant of balanced binary search tree with extra properties –red node’s children are always black, hence: longest path leaf-to-root is no more than twice shortest path guarantees O(log N) –lookups –insertions/deletions 6

Multimaps may store several elements with same key interface similar to maps does not provide indexing operator (does not make sense) lookup –elements with same key are stored together –find returns iterator to one of elements (not necessarily first) –upper_bound() and lower_bound() provide a range of iterators for particular key, specifically lower_bound(key) returns iterator to first element not smaller than key upper_bound(key) returns iterator to first element greater than key both return end() if key is not there –equal_range() returns a pair of iterators for the range erase() works on single iterator or range 7

Sets and Multisets key and value are the same –put another way: operates on scalar variables (not pairs) need and using std::set or using std::multiset keep elements sorted does not implement indexing (nothing to index) you can modify elements through iterators but you shouldn’t (destroys order) –in general, to update use erase() followed by insert() 8

Unordered Containers (Hashes) defined in C++11 hash container contains a sequence of buckets of elements hash function translates (hashes) key to bucket index collision – hashing different keys to same bucket –usually resolved through pointer to linked list of elements unordered map – needs and using std::unordered_map –same interface as map –plus: the implementation is transparent load_factor() – average number of elements per bucket bucket_count() – number of buckets max_load_factor () – maximum load factor before bucket count increases ( 1 by default) bucket(key) – bucket for key begin(key), end(key) returns local_iterator for the chain of elements in a bucket unordered set – needs and using std::unordered_set –same interface as set and above bucket functions unordered multimap and multiset exist with expected interfaces 9