STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –

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

1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE Lecture 15 – Maps.
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.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
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.
. 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.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
More on the STL vector list stack queue priority_queue.
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.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
Data Structures Using C++ 2E
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)
111 © 2002, Cisco Systems, Inc. All rights reserved.
Generic Programming Using the C++ Standard Template Library.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ STL CSCI 3110.
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.
LECTURE 36: DICTIONARY CSC 212 – Data Structures.
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
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:
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
 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.
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:
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
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.
The Standard Template Library Container Classes Version 1.0.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
C++ Review STL CONTAINERS.
Associative Containers Sets Maps Section 4.8. Associative Containers.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
Lecture 9 Binary Trees Trees General Definition Terminology
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.
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.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Sets and Multimaps 9/9/2017 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Standard Template Library (STL)
C++ Programming Standard Library
Chapter 9 – Sets and Maps 9.1 Associative Container
Sequences 11/12/2018 5:23 AM Sets Sets.
Associative Structures
Introduction to the Standard Template Library
A Sorted, Unique Key Container
Recitation Outline C++ STL associative containers Examples
Standard Template Library Model
Iterators and STL Containers
A dictionary lookup mechanism
Presentation transcript:

STL-Associative Containers

Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : – Store items by key: I have the word "apple" I have the word "carrot" – No numeric index – word[0] is meaningless

Sets Set – Stores set of items – One copy max of each value – Items stored according to < operator Can change

Set Demo

No indexes! No index access: Always need iterators:

Other Set Tools find(item) returns an iterator to item – Returns end() if not found erase() takes value or iterator

Multiset Same as Set, but duplicates allowed – Also in Set library

Grouping Things Complex groupings deserve struct/class – Class if has behaviors – Struct – simple data Do we really want lots of structs like  ?

Pair Pair template in library Looks like:

Pair Use Only has first/second Can "construct" with initializer list

Pair Use Can make a vector of pairs: But associations not explicitly stored – No order – No easy way to look up things

Map Stores a sorted list of key/value pairs Key A B C D E Value Apple Banana Carrot Dingo Elf

Map Key used as index into list – Assigning creates record Key A B C D E Value Apple Banana Carrot Dingo Elf

Map Gotcha Accessing a key adds it to the Map with default value

Ways to insert: Insertions – Map stores pairs, multiple ways to add:

Ways to insert: Each key can have only one value:

Iteration: Use iterators just like vector – Access parts with first/second Map stored ordered by keys 

Find/Contains Can find(keyType): – No find(valueType)

Erase Erase takes key or iterator: – Not a value

MultiMap To store list as value, could use map of vector: Can’t use [] with multimap

Multimap Equal_range returns pair of iterators – first : First location where we found a match – second : One past last location of match – first == second : not there