From 3f47154335fbace751ac29d919c86373d3e4781e Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 11 Mar 2025 02:06:50 -0400 Subject: [PATCH] renaming --- lectures/16_maps_II/README.md | 18 +++++--- .../map_insert/map_insert_copy.cpp | 41 +++++++++++++++++ .../map_insert/map_insert_move.cpp | 46 +++++++++++++++++++ 3 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 lectures/optimization/map_insert/map_insert_copy.cpp create mode 100644 lectures/optimization/map_insert/map_insert_move.cpp diff --git a/lectures/16_maps_II/README.md b/lectures/16_maps_II/README.md index a0b9b41..532776f 100644 --- a/lectures/16_maps_II/README.md +++ b/lectures/16_maps_II/README.md @@ -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 - Let’s 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 ``` -## 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). diff --git a/lectures/optimization/map_insert/map_insert_copy.cpp b/lectures/optimization/map_insert/map_insert_copy.cpp new file mode 100644 index 0000000..c6cc0bc --- /dev/null +++ b/lectures/optimization/map_insert/map_insert_copy.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +class LargeClass { +public: + std::vector 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 map_insert; + + for (int i = 0; i < map_size; ++i) { + map_insert.insert({i, LargeClass(num_elements, "Insert_" + std::to_string(i))}); + } + + return 0; +} + diff --git a/lectures/optimization/map_insert/map_insert_move.cpp b/lectures/optimization/map_insert/map_insert_move.cpp new file mode 100644 index 0000000..3ddefad --- /dev/null +++ b/lectures/optimization/map_insert/map_insert_move.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +class LargeClass { +public: + std::vector 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 map_insert; + + for (int i = 0; i < map_size; ++i) { + map_insert.insert({i, LargeClass(num_elements, "Insert_" + std::to_string(i))}); + } + + return 0; +} +