Todays 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 ID: 760274
Download Presentation The PPT/PDF document "STL Containers Some other containers in ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
STL Containers
Some other containers in the STL
Slide2Today’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?
Slide3Motivating Associative Containers
How can we find a value in a
std
::vector
?
Option 1:
Slide4Motivating 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?
Slide5Motivating 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
Slide6std::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
>
Slide7std::map’s insert function
insert(
std
::pair)
Will
attempt
to insert the value into the map
Returns a pair:
first – An iterator to the inserted element
second – true if the insertion took place, false otherwise
Slide8#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));}
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
Slide9#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"; }}
Searching std::map
map_lookup.cpp
What should we use to search, key or value?
Slide10#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"; }}
Searching std::map
map_lookup.cpp
How do we check if the key was found?
How do we check if the key was
not
found?
Slide11#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"; }}
Searching std::map
map_lookup.cpp
The iterator points to the entire node in the map, which is an
std::pair!
www.apple.com: 398782126
Could not find www.microsft.com
Slide12int 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"; }}
Iterating Over an std::map
Use iterators and a for loop to print out all the nodes in the std::map.
map_loop.cpp
www.apple.com
:
398782126
www.google.com:
215183441
www.utoronto.ca: 918567265
Slide13Accessing 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
Slide14std::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";
What will this output?
map_access.cpp
Microsoft: 0
Google: 215183441
Apple: 398782126
Ebay: 0
Missing keys inserted with default values!
Slide15#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";}
Duplicate Keys with Map
Key exists
. Not inserted!
John Smith earns $45000
Slide16#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";}
Handling Duplicate Keys with Multimap
An insertion takes place – this is a multimap.
Only one entry is
(arbitrarily)
returned.
Slide17#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"; }}
Looking Up Multiple Values in MultiMap
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
Slide18Other Containers
ContainerFindInsertEraseCommentsstd::mapO(log n)O(log n)O(log n)Use when you need the keys to be orderedstd::multimapO(log n)O(log n)O(log n)Use when you have duplicate keysstd::unordered_mapO(1)O(1)O(1)Use when the order of keys does not matterstd::setO(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