STL Containers Some other containers in the STL.

Slides:



Advertisements
Similar presentations
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Advertisements

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 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
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.
 2003 Prentice Hall, Inc. All rights reserved stack Adapter stack –Header –Insertions and deletions at one end –Last-in, first-out (LIFO) data.
Data Structures Using C++ 2E
Data Structure & Algorithm 09 – Binary Search Tree JJCAO.
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 : –
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
C++ STL CSCI 3110.
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.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
 200 Total Points ◦ 75 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 35 Points Short Answer ◦ 30 Points Multiple Choice.
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.
CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9.
STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
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.
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)
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.
Object-Oriented Programming (OOP) Lecture No. 41
Module 20/21/22: The Standard Template Library
Standard Template Library
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Standard Template Library (STL)
Basic Data Structures.
Introduction Applications Balance Factor Rotations Deletion Example
Functionality & Performance in Milestone 1
Compsci 201 Priority Queues & Autocomplete
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Dynamic Dictionaries Primary Operations: Additional operations:
Binary Search Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
What remains Topics Assignments Final exam
Basic Data Structures.
CMSC 341 Hashing (Continued)
Associative Structures
Some standard templated classes
Data Structures and Algorithms
MSIS 655 Advanced Business Applications Programming
CSE 373: Data Structures and Algorithms
CS212: Object Oriented Analysis and Design
Containers, Iterators, Algorithms, Thrust
Elements are always copied when they are put into a container
Recitation Outline C++ STL associative containers Examples
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Binary Search Trees.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
STL Iterators Separating Container from Data Access.
The Standard Template Library
STL Algorithms Examples of how to use some STL algorithms.
CH 9 : Maps And Dictionary
EE 312 Final Exam Review.
slides created by Marty Stepp
Functionality & Performance in Milestone 1
CO4301 – Advanced Games Development Week 12 Using Trees
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

STL Containers Some other containers in the STL

Today’s Questions What is an associative container? Why would I use an associative container over a sequential container? What is an unordered associative container? Why would I use an unordered associative container over an ordered one?

Motivating Associative Containers How can we find a value in a std::vector? Option 1:

Motivating Associative Containers How can we find a value in a std::vector? Option 1: std::find – which has O(n) complexity Option 2: sort the container, then search efficiently Difficult/error prone What data structure is inherently ordered?

Motivating Associative Containers How can we find a value in a std::vector? Option 1: std::find – which has O(n) complexity Option 2: sort the container, then search efficiently Difficult/error prone What data structure is inherently ordered? From ECE244: a binary search tree (BST) STL equivalent: std::map

std::map Benefits std::map uses std::pair Self-balancing BST Guaranteed O(log n): find, insert, and erase std::map uses std::pair First: key Second: value ECE244 Lab 5 – Domain Name Lookup Key: domain name as an std::string Value: IP address as an int std::pair<std::string, int>

std::map Insertion Example #include <map> #include <string> int main() { std::map<std::string, int> dns_lookup; // Option 1 std::pair<std::string, int> google("www.google.com", 215183441); dns_lookup.insert(google); // Option 2 auto apple = std::make_pair("www.apple.com", 398782126); dns_lookup.insert(apple); // Option 3 dns_lookup.insert(std::make_pair("www.utoronto.ca", 918567265)); } Include the map container Create the map, specifying the key and value Create pair first (constructor), then insert Create pair first (helper function), then insert Pass pair directly to insert map_insert.cpp

Searching std::map #include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair("www.google.com", 215183441)); dns_lookup.insert(std::make_pair("www.apple.com", 398782126)); dns_lookup.insert(std::make_pair("www.utoronto.ca", 918567265)); auto it = dns_lookup.find("www.apple.com"); if(it != dns_lookup.end()) { std::cout << it->first << ": " << it->second << "\n"; } it = dns_lookup.find("www.microsoft.com"); if(it == dns_lookup.end()) { std::cout << "Could not find www.microsoft.com\n"; What should we use to search, key or value? map_lookup.cpp

Searching std::map #include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair("www.google.com", 215183441)); dns_lookup.insert(std::make_pair("www.apple.com", 398782126)); dns_lookup.insert(std::make_pair("www.utoronto.ca", 918567265)); auto it = dns_lookup.find("www.apple.com"); if(it != dns_lookup.end()) { std::cout << it->first << ": " << it->second << "\n"; } it = dns_lookup.find("www.microsoft.com"); if(it == dns_lookup.end()) { std::cout << "Could not find www.microsoft.com\n"; How do we check if the key was found? How do we check if the key was not found? map_lookup.cpp

Searching std::map #include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair("www.google.com", 215183441)); dns_lookup.insert(std::make_pair("www.apple.com", 398782126)); dns_lookup.insert(std::make_pair("www.utoronto.ca", 918567265)); auto it = dns_lookup.find("www.apple.com"); if(it != dns_lookup.end()) { std::cout << it->first << ": " << it->second << "\n"; } it = dns_lookup.find("www.microsoft.com"); if(it == dns_lookup.end()) { std::cout << "Could not find www.microsoft.com\n"; www.apple.com: 398782126 The iterator points to the entire node in the map, which is an std::pair! Could not find www.microsft.com map_lookup.cpp

Iterating Over an std::map int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair("www.google.com", 215183441)); dns_lookup.insert(std::make_pair("www.apple.com", 398782126)); dns_lookup.insert(std::make_pair("www.utoronto.ca", 918567265)); for(auto it = dns_lookup.begin(); it != dns_lookup.end(); ++it) { std::cout << it->first << ": " << it->second << "\n"; } Use iterators and a for loop to print out all the nodes in the std::map. www.apple.com: 398782126 www.google.com: 215183441 www.utoronto.ca: 918567265 map_loop.cpp

Accessing Elements in std::map operator[key] Returns a reference to the value If key does not exist, will insert a new one Helpful as a replacement for insert Dangerous for lookup

What will this output? Google: 215183441 Microsoft: 0 Apple: 398782126 std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair("www.google.com", 215183441)); dns_lookup.insert(std::make_pair("www.apple.com", 398782126)); //dns_lookup.insert(std::make_pair("www.utoronto.ca", 918567265)); dns_lookup["www.utoronto.ca"] = 918567265; std::cout << "Google: " << dns_lookup["www.google.com"] << "\n"; std::cout << "Microsoft: " << dns_lookup["microsoft.com"] << "\n"; std::cout << "Apple: " << dns_lookup["www.apple.com"] << "\n"; std::cout << "Ebay: " << dns_lookup.["ebay.ca"] << "\n"; Google: 215183441 Microsoft: 0 Apple: 398782126 Ebay: 0 Missing keys inserted with default values! map_access.cpp

Duplicate Keys with Map #include <map> #include <iostream> int main() { std::map<std::string, float> salary_database; salary_database.insert(std::make_pair("John Smith", 45000)); salary_database.insert(std::make_pair("Mary Sue", 90000)); salary_database.insert(std::make_pair("Gary Stu", 40000)); salary_database.insert(std::make_pair("John Smith", 30000)); auto it = salary_database.find("John Smith"); std::cout << it->first " earns $" it->second << "\n"; } Key exists. Not inserted! John Smith earns $45000

Handling Duplicate Keys with Multimap #include <map> #include <iostream> int main() { std::multimap<std::string, float> salary_database; salary_database.insert(std::make_pair("John Smith", 45000)); salary_database.insert(std::make_pair("Mary Sue", 90000)); salary_database.insert(std::make_pair("Gary Stu", 40000)); salary_database.insert(std::make_pair("John Smith", 30000)); auto it = salary_database.find("John Smith"); std::cout << it->first " earns $" it->second << "\n"; } An insertion takes place – this is a multimap. Only one entry is (arbitrarily) returned.

Looking Up Multiple Values in MultiMap #include <map> #include <iostream> int main() { std::multimap<std::string, float> salary_database; salary_database.insert(std::make_pair("John Smith", 45000)); salary_database.insert(std::make_pair("Mary Sue", 90000)); salary_database.insert(std::make_pair("Gary Stu", 40000)); salary_database.insert(std::make_pair("John Smith", 30000)); auto range = salary_database.equal_range("John Smith"); for(auto it = range.first; it != range.second; ++it) { std::cout << it->first " earns $" it->second << "\n"; } equal_range returns a std::pair<iterator, iterator> that gives the range of entries matching the key “John Smith” John Smith earns $30000 John Smith earns $45000

Other Containers Container Find Insert Erase Comments std::map O(log n) Use when you need the keys to be ordered std::multimap Use when you have duplicate keys std::unordered_map O(1) Use when the order of keys does not matter std::set Use when you only need keys and not their values Container Push Pop Comments std::queue O(1) Use when you want first-in, first-out (FIFO) semantics std::priority_queue O(log n) Keeps elements ordered. Use when you want fast look-up of the largest (by default) element. std::stack Use when you want last-in, first-out (LIFO) semantics std::deque Similar to a vector, but can be faster if there are frequent insertions/deletions