This commit is contained in:
Jidong Xiao
2025-03-11 02:06:50 -04:00
committed by JamesFlare1212
parent 94e66529f4
commit 3f47154335
3 changed files with 98 additions and 7 deletions

View File

@@ -1,6 +1,10 @@
# Lecture 16 --- Associative Containers (Maps), Part 2
## 16.1 Map Erase Performance
## 16.1 Map Insert Performance
One way to improve the map insert performance is using the move constructor. Read [this document](../optimization/map_insert).
## 16.2 Map Erase Performance
We learned from previous lecture that we can erase an element from a map via the erase function, and there are two formats:
@@ -8,9 +12,9 @@ We learned from previous lecture that we can erase an element from a map via the
- size_type erase(const key_type& k) — erase the pair containing key k, returning either 0 or 1, depending on whether or not the key was in a pair in the map.
But there is a performance difference between these two. Read [this document](..//optimization/map_erase/).
But there is a performance difference between these two. Read [this document](../optimization/map_erase/).
## 16.2 More Complicated Values
## 16.3 More Complicated Values
- Lets look at the example:
```cpp
@@ -51,7 +55,7 @@ use of push back or through construction of the vector).
- We can figure out the correct syntax for all of these by drawing pictures to visualize the contents of the map
and the pairs stored in the map. We will do this during lecture, and you should do so all the time in practice.
## 16.3 Typedefs
## 16.5 Typedefs
- One of the painful aspects of using maps is the syntax. For example, consider a constant iterator in a map
associating strings and vectors of ints:
@@ -75,7 +79,7 @@ map_vect :: const_iterator p;
The compiler makes the substitution for you.
## 16.4 Standard Library Sets
## 16.6 Standard Library Sets
- STL sets are ordered containers storing unique “keys”. An ordering relation on the keys, which defaults to
operator<, is necessary. Because STL sets are ordered, they are technically not traditional mathematical sets.
@@ -89,12 +93,12 @@ you shouldn't use [] to access elements in a set.
#include <set>
```
## 16.5 Leetcode Exercises (Maps)
## 16.7 Leetcode Exercises (Maps)
- [Leetcode problem 49: Group Anagrams](https://leetcode.com/problems/group-anagrams/). Solution: [p49_group_anagrams.cpp](../../leetcode/p49_group_anagrams.cpp).
- [Leetcode problem 290: Word Pattern](https://leetcode.com/problems/word-pattern/). Solution: [p290_word_pattern.cpp](../../leetcode/p290_word_pattern.cpp).
## 16.6 Leetcode Exercises (Sets)
## 16.8 Leetcode Exercises (Sets)
- [Leetcode problem 414: Third Maximum Number](https://leetcode.com/problems/third-maximum-number/). Solution: [p414_third_max_number.cpp](../../leetcode/p414_third_max_number.cpp).
- [Leetcode problem 1207: Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/). Solution: [p1207_unique_number_occurrences.cpp](../../leetcode/p1207_unique_number_occurrences.cpp).

View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <map>
#include <vector>
#include <string>
class LargeClass {
public:
std::vector<int> data;
std::string name;
LargeClass(int size, const std::string& n) : data(size), name(n) {
// simulate work in the constructor
for (int i = 0; i < size; ++i) {
data[i] = i;
}
}
// define a copy constructor for demonstration purposes
LargeClass(const LargeClass& other) : data(other.data), name(other.name) {
// std::cout << "Copy constructor called\n";
}
// for demonstration, printing the object contents
void print() const {
std::cout << name << ": " << data[0] << "..." << data[data.size() - 1] << std::endl;
}
};
int main() {
const int num_elements = 10000000;
const int map_size = 10;
std::map<int, LargeClass> map_insert;
for (int i = 0; i < map_size; ++i) {
map_insert.insert({i, LargeClass(num_elements, "Insert_" + std::to_string(i))});
}
return 0;
}

View File

@@ -0,0 +1,46 @@
#include <iostream>
#include <map>
#include <vector>
#include <string>
class LargeClass {
public:
std::vector<int> data;
std::string name;
LargeClass(int size, const std::string& n) : data(size), name(n) {
// simulate work in the constructor
for (int i = 0; i < size; ++i) {
data[i] = i;
}
}
// define a copy constructor for demonstration purposes
LargeClass(const LargeClass& other) : data(other.data), name(other.name) {
// std::cout << "Copy constructor called\n";
}
// define a move constructor
LargeClass(LargeClass&& other) noexcept : data(std::move(other.data)), name(std::move(other.name)) {
// std::cout << "Move constructor called\n";
}
// for demonstration, printing the object contents
void print() const {
std::cout << name << ": " << data[0] << "..." << data[data.size() - 1] << std::endl;
}
};
int main() {
const int num_elements = 10000000;
const int map_size = 10;
std::map<int, LargeClass> map_insert;
for (int i = 0; i < map_size; ++i) {
map_insert.insert({i, LargeClass(num_elements, "Insert_" + std::to_string(i))});
}
return 0;
}