diff --git a/lectures/15_maps_I/README.md b/lectures/15_maps_I/README.md index e85ed95..937722d 100644 --- a/lectures/15_maps_I/README.md +++ b/lectures/15_maps_I/README.md @@ -1,38 +1,85 @@ # Lecture 15 --- Associative Containers (Maps), Part 1 Associative Containers (STL Maps) +- STL Pairs - STL Maps: associative containers for fast insert, access and remove - Example: Counting word occurrences -- STL Pairs - Map iterators - Map member functions: operator[], find, insert, erase. - Efficiency - STL maps vs. STL vectors vs. STL lists -## 15.1 STL Maps: Associative Containers +## 15.1 STL Pairs + +- std::pair is a templated struct with just two members, called first and second. + +```cpp +template +struct pair { + T1 first; + T2 second; + + // Constructors + constexpr pair(); + constexpr pair(const T1& x, const T2& y); + + // Other member functions... +}; +``` + +**struct** in C++ is identical to class except that members are public by default, std::pair is implemented as a struct to allow direct access to its first and second members without requiring explicit getter functions. +- To work with pairs, you must #include <utility>. Note that the header file for maps (#include <map>) itself includes utility, so you don't have to include utility explicitly when you use pairs with maps. +- Here are simple examples of manipulating pairs: +```cpp +std::pair p1(5, 7.5); +std::pair p2 = std::make_pair(8, 9.5); +p1.first = p2.first; +p2.second = 13.3; +std::cout << p1.first << " " << p1.second << std::endl; +std::cout << p2.first << " " << p2.second << std::endl; +p1 = p2; +std::pair p3 = std::make_pair(std::string("hello"), 3.5); +p3.second = -1.5; +// p3.first = std::string("illegal"); // (a) +// p1 = p3; // (b) +``` +- The function std::make_pair creates a pair object from the given values. It is really just a simplified +constructor, and as the example shows there are other ways of constructing pairs. +- Most of the statements in the above code show accessing and changing values in pairs. +The two statements at the end are commented out because they cause syntax errors: +- In (a), the first entry of p3 is const, which means it can’t be changed. +- In (b), the two pairs are different types! Make sure you understand this. +- Returning to maps, each entry in the map is a pair object of type: +std::pair<const key_type, value_type> +- The const is needed to ensure that the keys aren’t changed! This is crucial because maps are sorted by keys! + +## 15.2 STL Maps: Associative Containers - STL maps store pairs of “associated” values. -- An association between a string, representing a word, and an int representing the number of times that +- e.g., An association between a string, representing a word, and an int representing the number of times that word has been seen in an input file. -- An association between a string, representing a word, and a vector that stores the line numbers from a -text file on which that string occurs (next lecture). -- An association between a phone number and the name of the person with that number (Lab 9). -- An association between a class object representing a student name and the student’s info (next lecture). +- e.g., An association between a string, representing a word, and a vector that stores the line numbers from a +text file on which that string occurs. +- e.g., An association between a phone number and the name of the person with that number. +- e.g., An association between a class object representing a student name and the student’s info. A particular instance of a map is defined (declared) with the syntax: + +```cpp std::map<key_type, value_type> var_name -In our first two examples above, key type is a string. In the first example, the value type is an int and in +``` +- In our first two examples above, key type is a string. In the first example, the value type is an int and in the second it is a std::vector<int>. - Entries in maps are pairs: std::pair<const key_type, value_type>, or just std::pair<key_type, value_type>. - Map iterators refer to pairs. - Map search, insert and erase are all very fast: O(log n) time, where n is the number of pairs stored in the map. - Note: The STL map type has similarities to the Python dictionary, Java HashMap, or a Perl hash, but the + - Map search, insert and erase are O(log n). First, let’s see how some of this works with a program to count the occurrences of each word in a file. We’ll look at more details and more examples later. -## 15.2 Counting Word Occurrences +## 15.3 Counting Word Occurrences Here’s a simple and elegant solution to this problem using a map: @@ -64,36 +111,6 @@ must be an operator< defined for the key. changed. It can only be erased (together with the associated value). - Duplicate keys can not be in the map. -## 15.4 STL Pairs - -The mechanics of using std::pairs are relatively straightforward: -- std::pairs are a templated struct with just two members, called first and second. struct in C++ is identical to class except that members are public by default, std::pair is implemented as a struct to allow direct access to its first and second members without requiring explicit getter functions. -- To work with pairs, you must #include <utility>. Note that the header file for maps (#include <map>) -itself includes utility, so you don’t have to include utility explicitly when you use pairs with maps. -- Here are simple examples of manipulating pairs: -```cpp -std::pair p1(5, 7.5); -std::pair p2 = std::make_pair(8, 9.5); -p1.first = p2.first; -p2.second = 13.3; -std::cout << p1.first << " " << p1.second << std::endl; -std::cout << p2.first << " " << p2.second << std::endl; -p1 = p2; -std::pair p3 = std::make_pair(std::string("hello"), 3.5); -p3.second = -1.5; -// p3.first = std::string("illegal"); // (a) -// p1 = p3; // (b) -``` -- The function std::make_pair creates a pair object from the given values. It is really just a simplified -constructor, and as the example shows there are other ways of constructing pairs. -- Most of the statements in the above code show accessing and changing values in pairs. -The two statements at the end are commented out because they cause syntax errors: -- In (a), the first entry of p3 is const, which means it can’t be changed. -- In (b), the two pairs are different types! Make sure you understand this. -- Returning to maps, each entry in the map is a pair object of type: -std::pair<const key_type, value_type> -- The const is needed to ensure that the keys aren’t changed! This is crucial because maps are sorted by keys! - ## 15.5 Maps: operator[] - We’ve used the [] operator on vectors, which is conceptually very simple because vectors are just resizable