0% found this document useful (0 votes)
91 views19 pages

More STL Containers

This document discusses associative containers in the C++ STL. It explains that associative containers like std::map provide fast lookup of values based on keys due to being implemented as self-balancing binary search trees. It provides examples of inserting elements into and searching in a std::map. It also discusses using std::multimap to allow duplicate keys and iterating over the elements of a std::map.

Uploaded by

danielkooeun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views19 pages

More STL Containers

This document discusses associative containers in the C++ STL. It explains that associative containers like std::map provide fast lookup of values based on keys due to being implemented as self-balancing binary search trees. It provides examples of inserting elements into and searching in a std::map. It also discusses using std::multimap to allow duplicate keys and iterating over the elements of a std::map.

Uploaded by

danielkooeun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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
• 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 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

What should we use to search, key or value?

map_lookup.cpp
Searching std::map

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

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

Use iterators and a for loop to print out all the nodes in the std::map.
map_loop.cpp
Iterating Over an std::map

www.google.com: 215183441
www.apple.com: 398782126
map_loop.cpp

www.utoronto.ca: 918567265
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?

map_access.cpp
What will this output?

Google: 215183441
Microsoft: 0
Apple: 398782126
Ebay: 0

map_access.cpp
Duplicate Keys

Key exists.
Not inserted!

John Smith earns $45000


Handling Duplicate Keys with Multimap

An insertion
takes place –
this is a
multimap.

Only one entry is


(arbitrarily) returned.
Using Multimap Correctly

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) O(log n) O(log n) Use when you need the keys to be ordered
std::multimap O(log n) O(log n) O(log n) Use when you have duplicate keys
std::unordered_map O(1) O(1) O(1) Use when the order of keys does not matter
std::set O(log n) O(log n) O(log n) Use when you only need keys and not their values

Container Push Pop Comments


std::queue O(1) O(1) Use when you want first-in, first-out (FIFO) semantics
std::priority_queue O(log n) O(1) Keeps elements ordered. Use when you want fast look-up of
the largest (by default) element.
std::stack O(1) O(1) Use when you want last-in, first-out (LIFO) semantics
std::deque O(1) O(1) Similar to a vector, but can be faster if there are frequent
insertions/deletions

You might also like