Download presentation
Presentation is loading. Please wait.
Published byArchibald Eaton Modified over 5 years ago
1
STL Containers Some other containers in the STL
2
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?
3
Motivating Associative Containers
How can we find a value in a std::vector? Option 1:
4
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?
5
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
6
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>
7
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(" ); dns_lookup.insert(google); // Option 2 auto apple = std::make_pair(" ); dns_lookup.insert(apple); // Option 3 dns_lookup.insert(std::make_pair(" )); } 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
8
Searching std::map #include <iostream> #include <map>
#include <string> int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); auto it = dns_lookup.find(" if(it != dns_lookup.end()) { std::cout << it->first << ": " << it->second << "\n"; } it = dns_lookup.find(" if(it == dns_lookup.end()) { std::cout << "Could not find What should we use to search, key or value? map_lookup.cpp
9
Searching std::map #include <iostream> #include <map>
#include <string> int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); auto it = dns_lookup.find(" if(it != dns_lookup.end()) { std::cout << it->first << ": " << it->second << "\n"; } it = dns_lookup.find(" if(it == dns_lookup.end()) { std::cout << "Could not find How do we check if the key was found? How do we check if the key was not found? map_lookup.cpp
10
Searching std::map #include <iostream> #include <map>
#include <string> int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); auto it = dns_lookup.find(" if(it != dns_lookup.end()) { std::cout << it->first << ": " << it->second << "\n"; } it = dns_lookup.find(" if(it == dns_lookup.end()) { std::cout << "Could not find The iterator points to the entire node in the map, which is an std::pair! Could not find map_lookup.cpp
11
Iterating Over an std::map
int main() { std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); 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. map_loop.cpp
12
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
13
What will this output? Google: 215183441 Microsoft: 0 Apple: 398782126
std::map<std::string, int> dns_lookup; dns_lookup.insert(std::make_pair(" )); dns_lookup.insert(std::make_pair(" )); //dns_lookup.insert(std::make_pair(" )); dns_lookup[" = ; std::cout << "Google: " << dns_lookup[" << "\n"; std::cout << "Microsoft: " << dns_lookup["microsoft.com"] << "\n"; std::cout << "Apple: " << dns_lookup[" << "\n"; std::cout << "Ebay: " << dns_lookup.["ebay.ca"] << "\n"; Google: Microsoft: 0 Apple: Ebay: 0 Missing keys inserted with default values! map_access.cpp
14
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
15
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.
16
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
17
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.