move pairs up

This commit is contained in:
Jidong Xiao
2025-02-28 13:22:36 -05:00
committed by JamesFlare1212
parent 23acdc2bea
commit 086aad56cf

View File

@@ -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 <typename T1, typename T2>
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 &lt;utility&gt;. Note that the header file for maps (#include &lt;map&gt;) 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<int, double> p1(5, 7.5);
std::pair<int, double> 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<const std::string, double> 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 cant 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&lt;const key_type, value_type&gt;
- The const is needed to ensure that the keys arent 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 students 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 students info.
A particular instance of a map is defined (declared) with the syntax:
```cpp
std::map&lt;key_type, value_type&gt; 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&lt;int&gt;.
- Entries in maps are pairs: std::pair&lt;const key_type, value_type&gt;, or just std::pair&lt;key_type, value_type&gt;.
- 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
<!-- Note: The STL map type has similarities to the Python dictionary, Java HashMap, or a Perl hash, but the
data structures are not the same. The organization, implementation, and performance is different. In a couple
weeks well see an STL data structure that is even more similar to the Python dictionary.
weeks well see an STL data structure that is even more similar to the Python dictionary.-->
- Map search, insert and erase are O(log n).
First, lets see how some of this works with a program to count the occurrences of each word in a file. Well look
at more details and more examples later.
## 15.2 Counting Word Occurrences
## 15.3 Counting Word Occurrences
Heres 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 &lt;utility&gt;. Note that the header file for maps (#include &lt;map&gt;)
itself includes utility, so you dont have to include utility explicitly when you use pairs with maps.
- Here are simple examples of manipulating pairs:
```cpp
std::pair<int, double> p1(5, 7.5);
std::pair<int, double> 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<const std::string, double> 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 cant 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&lt;const key_type, value_type&gt;
- The const is needed to ensure that the keys arent changed! This is crucial because maps are sorted by keys!
## 15.5 Maps: operator[]
- Weve used the [] operator on vectors, which is conceptually very simple because vectors are just resizable