diff --git a/org/cpp/containers.org b/org/cpp/containers.org new file mode 100644 index 0000000..a5e710c --- /dev/null +++ b/org/cpp/containers.org @@ -0,0 +1,109 @@ +#+title: C++ Containers + +* Container Concept: Common Interface :containers:concept: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504002 +:END: +** Front +What interface is shared by all C++ standard containers? +** Back +- *Element access*: ~at~, ~operator[]~, ~front~, ~back~, ~data~ +- *Iterators*: ~begin~, ~end~, ~cbegin~, ~cend~, ~rbegin~, ~rend~ +- *Capacity*: ~empty~, ~size~, ~max_size~ +- *Modifiers*: ~clear~, ~insert~, ~erase~, ~push_back~, ~pop_back~, ~resize~, ~swap~ + +* Do C++ containers inherit from a common base? :containers:concept:inheritance: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504003 +:END: +** Front +Do C++ containers (vector, list, map, etc.) inherit from a common base class? +** Back +No. Standard containers are standalone classes. Shared interfaces come from conventions and C++20 concepts (~Container~, ~SequenceContainer~), not inheritance. + +* Sequence Containers :containers:sequence: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504004 +:END: +** Front +What are the C++ sequence containers? +** Back +~vector~, ~deque~, ~list~, ~forward_list~, ~array~ + +Sequence containers maintain insertion order. + +* Associative Containers :containers:associative: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504005 +:END: +** Front +What are the C++ associative containers? +** Back +- *Ordered*: ~set~, ~multiset~, ~map~, ~multimap~ +- *Unordered*: ~unordered_set~, ~unordered_multiset~, ~unordered_map~, ~unordered_multimap~ + +* Container Adapters :containers:adapter: +:PROPERTIES: +:ANKI_NOTE_ID: 1777826871054 +:END: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504006 +** Front +What are the C++ container adapters? +** Back +~stack~, ~queue~, ~priority_queue~ + +Adapters provide a restricted interface (e.g., LIFO for stack, FIFO for queue). + +* What does vector have that other containers don't? :containers:vector: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504007 +:END: +** Front +Which methods does ~vector~ have that other containers typically don't? +** Back +~reserve~, ~capacity~, ~shrink_to_fit~ + +These manage the internal memory buffer. + +* Unique methods on sequence containers (list, deque) :containers:sequence: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504008 +:END: +** Front +Which sequence containers support ~push_front~ and ~pop_front~? +** Back +~deque~ and ~list~ (not ~vector~) + +These containers support insertion/removal at both ends. + +* Unique methods on associative containers :containers:associative: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504009 +:END: +** Front +What tree-based methods do ordered associative containers have? +** Back +~find~, ~count~, ~lower_bound~, ~upper_bound~, ~equal_range~ + +These use tree traversal (BST behind the scenes). + +* Unique methods on unordered containers :containers:unordered: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504010 +:END: +** Front +What hash-specific methods do unordered containers have? +** Back +~bucket_count~, ~load_factor~, ~max_load_factor~, ~rehash~, ~reserve~ + +These manage hash table internals. diff --git a/org/cpp/deque.org b/org/cpp/deque.org index 82a4f40..dc4a850 100644 --- a/org/cpp/deque.org +++ b/org/cpp/deque.org @@ -1,6 +1,7 @@ * Task: construct an empty deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650432 :END: ** Front Write how to create an empty deque of ints @@ -12,6 +13,7 @@ std::deque d; * Task: construct deque with 5 copies of value :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650427 :END: ** Front Write how to create a deque with 5 elements, all initialized to 42 @@ -23,6 +25,7 @@ std::deque d(5, 42); // {42, 42, 42, 42, 42} * Task: construct deque from initializer list :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650422 :END: ** Front Write how to create a deque initialized with {1, 2, 3} @@ -34,6 +37,7 @@ std::deque d = {1, 2, 3}; * Task: construct deque from a range :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650418 :END: ** Front Write how to create a deque from a vector's begin/end iterators @@ -46,6 +50,7 @@ std::deque d(v.begin(), v.end()); * Task: access element at index with bounds checking :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650414 :END: ** Front Write how to safely access element at index 2 in a deque (throws on invalid index) @@ -58,6 +63,7 @@ int x = d.at(2); // returns 30, throws std::out_of_range if invalid * Task: access element at index with operator[] :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650409 :END: ** Front Write how to access element at index 1 using operator[] @@ -70,6 +76,7 @@ int x = d[1]; // returns 20, no bounds check * Task: access first and last elements :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650404 :END: ** Front Write how to get the first and last element of a deque @@ -83,6 +90,7 @@ int last = d.back(); // 30 * Task: iterate over deque with begin/end :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650399 :END: ** Front Write a range-based for loop over a deque using iterators @@ -99,6 +107,7 @@ for (int x : d) { std::cout << x << " "; } * Task: reverse iterate over deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650395 :END: ** Front Write how to iterate backwards through a deque using reverse iterators @@ -113,6 +122,7 @@ for (auto it = d.rbegin(); it != d.rend(); ++it) { * Task: check if deque is empty :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650391 :END: ** Front Write how to check if a deque is empty @@ -125,6 +135,7 @@ if (d.empty()) { /* ... */ } * Task: get size of deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650387 :END: ** Front Write how to get the number of elements in a deque @@ -137,6 +148,7 @@ std::size_t n = d.size(); // returns 3 * Task: clear all elements from deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650383 :END: ** Front Write how to remove all elements from a deque @@ -149,6 +161,7 @@ d.clear(); // size becomes 0 * Task: push elements to back of deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650379 :END: ** Front Write how to add elements 10, 20, 30 to the back of a deque @@ -163,6 +176,7 @@ d.push_back(30); * Task: push elements to front of deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650375 :END: ** Front Write how to add elements 30, 20, 10 to the front of a deque (pushing in that order) @@ -177,6 +191,7 @@ d.push_front(10); // {10, 20, 30} * Task: pop elements from back of deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650371 :END: ** Front Write how to remove the last element from a deque @@ -189,6 +204,7 @@ d.pop_back(); // {1, 2} * Task: pop elements from front of deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650367 :END: ** Front Write how to remove the first element from a deque @@ -201,6 +217,7 @@ d.pop_front(); // {2, 3} * Task: emplace back into deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650363 :END: ** Front Write how to construct a pair in-place at the back of a deque (without copying) @@ -213,6 +230,7 @@ d.emplace_back(1, 2); // constructs pair{1,2} directly * Task: emplace front into deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650359 :END: ** Front Write how to construct an element in-place at the front of a deque @@ -225,6 +243,7 @@ d.emplace_front("hello"); // constructs string directly at front * Task: resize deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650354 :END: ** Front Write how to resize a deque to 10 elements, filling new spots with 0 @@ -238,6 +257,7 @@ d.resize(5, 42); // resize to 5, filling with 42 if growing * Task: insert element at position :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650349 :END: ** Front Write how to insert value 99 at the beginning of a deque @@ -250,6 +270,7 @@ d.insert(d.begin(), 99); // {99, 1, 2, 3} * Task: insert multiple copies :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650344 :END: ** Front Write how to insert 5 copies of value 42 starting at position begin @@ -262,6 +283,7 @@ d.insert(d.begin(), 5, 42); // {42, 42, 42, 42, 42, 1, 2, 3} * Task: insert range into deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650339 :END: ** Front Write how to insert all elements from a vector into a deque at position begin @@ -275,6 +297,7 @@ d.insert(d.begin(), v.begin(), v.end()); // {10, 20, 1, 2, 3} * Task: erase single element :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650335 :END: ** Front Write how to erase the element at position begin + 1 in a deque @@ -287,6 +310,7 @@ d.erase(d.begin() + 1); // {1, 3} * Task: erase range of elements :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650331 :END: ** Front Write how to erase elements from begin to begin+2 (2 elements) in a deque @@ -299,6 +323,7 @@ d.erase(d.begin(), d.begin() + 2); // {3, 4, 5} * Task: swap two deques :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650326 :END: ** Front Write how to swap the contents of two deques @@ -312,6 +337,7 @@ d1.swap(d2); // d1 = {4,5,6}, d2 = {1,2,3} * Task: emplace at specific position :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650322 :END: ** Front Write how to construct a pair in-place at position begin of a deque @@ -324,6 +350,7 @@ d.emplace(d.begin(), 1, 2); // constructs pair{1,2} at begin * Task: assign new contents to deque :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650318 :END: ** Front Write how to replace all contents of a deque with 5 copies of 99 @@ -336,6 +363,7 @@ d.assign(5, 99); // {99, 99, 99, 99, 99} * Task: assign from range :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650314 :END: ** Front Write how to replace deque contents with all elements from a vector @@ -349,6 +377,7 @@ d.assign(v.begin(), v.end()); // {10, 20, 30} * Task: use deque as FIFO queue :cpp:deque:production: :PROPERTIES: :ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 1777825650305 :END: ** Front Write how to use deque as a FIFO queue (enqueue 1, 2, 3 then dequeue all) @@ -364,4 +393,4 @@ while (!q.empty()) { q.pop_front(); // dequeue } // Output: 1 2 3 -#+end_src \ No newline at end of file +#+end_src diff --git a/org/cpp/iterator.org b/org/cpp/iterator.org index 1eb9d70..ca7b443 100644 --- a/org/cpp/iterator.org +++ b/org/cpp/iterator.org @@ -44,6 +44,9 @@ What are the four required operations for a C++ iterator? ** Front List the five iterator categories in C++ (from simplest to most advanced) ** Back +:PROPERTIES: +:ANKI_NOTE_ID: 1777826854918 +:END: 1. Input Iterator — read values, advance once 2. Output Iterator — write values, advance once 3. Forward Iterator — read/write, multiple passes diff --git a/org/cpp/tricks/tricks.org b/org/cpp/tricks/tricks.org new file mode 100644 index 0000000..3a5625e --- /dev/null +++ b/org/cpp/tricks/tricks.org @@ -0,0 +1,17 @@ +#+title: C++ Tricks + +* Rotate String Check :string:trick:search: +:PROPERTIES: +:ANKI_NOTE_TYPE: Basic +:ANKI_NOTE_ID: 20260504001 +:END: +** Front +How to check if string ~s~ can be rotated to match string ~goal~ in O(n)? +** Back +#+begin_src c++ +bool rotateString(string s, string goal) { + return s.size() == goal.size() && (s + s).find(goal) != string::npos; +} +#+end_src + +If ~goal~ is a substring of ~s + s~, then ~s~ can be rotated to match ~goal~. \ No newline at end of file