From b26a3d31f750409f7fe1fd0ff09d2d63013df599 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 11 Feb 2025 13:44:29 -0500 Subject: [PATCH] add the comparison --- lectures/11_list_implementation/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lectures/11_list_implementation/README.md b/lectures/11_list_implementation/README.md index c726edc..5afe45a 100644 --- a/lectures/11_list_implementation/README.md +++ b/lectures/11_list_implementation/README.md @@ -5,6 +5,18 @@ - Our own version of the STL list<T> class, named dslist, Implementing list iterators - Common mistakes, STL List w/ iterators vs. “homemade” linked list with Node objects & pointers +## 11.0 When to Use `std::list` vs. `std::vector` + +| Use Case | Prefer `std::vector` | Prefer `std::list` | +|----------------------------------------------|----------------------|--------------------| +| **Frequent insertions/removals at arbitrary positions** | ❌ | ✅ | +| **Fast random access required** | ✅ | ❌ | +| **Memory efficiency is important** | ✅ | ❌ | +| **Iterators should remain valid** | ❌ | ✅ | +| **Heavy insertions/deletions at the end** | ✅ | ✅ (but `std::vector` is better) | +| **Cache locality and performance are important** | ✅ | ❌ | +| **Large number of elements (better memory utilization)** | ✅ | ❌ | + ## 11.1 Basic Linked Lists Mechanisms: Common Mistakes - Here is a summary of common mistakes. Read these carefully, and read them again when you have problem that