feat: add std::deque flashcards covering all member functions
This commit is contained in:
@@ -0,0 +1,367 @@
|
||||
* Task: construct an empty deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to create an empty deque of ints
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d;
|
||||
#+end_src
|
||||
|
||||
* Task: construct deque with 5 copies of value :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to create a deque with 5 elements, all initialized to 42
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d(5, 42); // {42, 42, 42, 42, 42}
|
||||
#+end_src
|
||||
|
||||
* Task: construct deque from initializer list :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to create a deque initialized with {1, 2, 3}
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
#+end_src
|
||||
|
||||
* Task: construct deque from a range :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to create a deque from a vector's begin/end iterators
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::vector<int> v = {1, 2, 3, 4, 5};
|
||||
std::deque<int> d(v.begin(), v.end());
|
||||
#+end_src
|
||||
|
||||
* Task: access element at index with bounds checking :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to safely access element at index 2 in a deque (throws on invalid index)
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {10, 20, 30};
|
||||
int x = d.at(2); // returns 30, throws std::out_of_range if invalid
|
||||
#+end_src
|
||||
|
||||
* Task: access element at index with operator[] :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to access element at index 1 using operator[]
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {10, 20, 30};
|
||||
int x = d[1]; // returns 20, no bounds check
|
||||
#+end_src
|
||||
|
||||
* Task: access first and last elements :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to get the first and last element of a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {10, 20, 30};
|
||||
int first = d.front(); // 10
|
||||
int last = d.back(); // 30
|
||||
#+end_src
|
||||
|
||||
* Task: iterate over deque with begin/end :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write a range-based for loop over a deque using iterators
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
for (auto it = d.begin(); it != d.end(); ++it) {
|
||||
std::cout << *it << " ";
|
||||
}
|
||||
// or:
|
||||
for (int x : d) { std::cout << x << " "; }
|
||||
#+end_src
|
||||
|
||||
* Task: reverse iterate over deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to iterate backwards through a deque using reverse iterators
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
for (auto it = d.rbegin(); it != d.rend(); ++it) {
|
||||
std::cout << *it << " "; // prints 3 2 1
|
||||
}
|
||||
#+end_src
|
||||
|
||||
* Task: check if deque is empty :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to check if a deque is empty
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d;
|
||||
if (d.empty()) { /* ... */ }
|
||||
#+end_src
|
||||
|
||||
* Task: get size of deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to get the number of elements in a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
std::size_t n = d.size(); // returns 3
|
||||
#+end_src
|
||||
|
||||
* Task: clear all elements from deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to remove all elements from a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.clear(); // size becomes 0
|
||||
#+end_src
|
||||
|
||||
* Task: push elements to back of deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to add elements 10, 20, 30 to the back of a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d;
|
||||
d.push_back(10);
|
||||
d.push_back(20);
|
||||
d.push_back(30);
|
||||
#+end_src
|
||||
|
||||
* Task: push elements to front of deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to add elements 30, 20, 10 to the front of a deque (pushing in that order)
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d;
|
||||
d.push_front(30); // {30}
|
||||
d.push_front(20); // {20, 30}
|
||||
d.push_front(10); // {10, 20, 30}
|
||||
#+end_src
|
||||
|
||||
* Task: pop elements from back of deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to remove the last element from a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.pop_back(); // {1, 2}
|
||||
#+end_src
|
||||
|
||||
* Task: pop elements from front of deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to remove the first element from a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.pop_front(); // {2, 3}
|
||||
#+end_src
|
||||
|
||||
* Task: emplace back into deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to construct a pair in-place at the back of a deque (without copying)
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<std::pair<int,int>> d;
|
||||
d.emplace_back(1, 2); // constructs pair{1,2} directly
|
||||
#+end_src
|
||||
|
||||
* Task: emplace front into deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to construct an element in-place at the front of a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<std::string> d;
|
||||
d.emplace_front("hello"); // constructs string directly at front
|
||||
#+end_src
|
||||
|
||||
* Task: resize deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to resize a deque to 10 elements, filling new spots with 0
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.resize(10); // {1, 2, 3, 0, 0, 0, 0, 0, 0, 0}
|
||||
d.resize(5, 42); // resize to 5, filling with 42 if growing
|
||||
#+end_src
|
||||
|
||||
* Task: insert element at position :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to insert value 99 at the beginning of a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.insert(d.begin(), 99); // {99, 1, 2, 3}
|
||||
#+end_src
|
||||
|
||||
* Task: insert multiple copies :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to insert 5 copies of value 42 starting at position begin
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.insert(d.begin(), 5, 42); // {42, 42, 42, 42, 42, 1, 2, 3}
|
||||
#+end_src
|
||||
|
||||
* Task: insert range into deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to insert all elements from a vector into a deque at position begin
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
std::vector<int> v = {10, 20};
|
||||
d.insert(d.begin(), v.begin(), v.end()); // {10, 20, 1, 2, 3}
|
||||
#+end_src
|
||||
|
||||
* Task: erase single element :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to erase the element at position begin + 1 in a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.erase(d.begin() + 1); // {1, 3}
|
||||
#+end_src
|
||||
|
||||
* Task: erase range of elements :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to erase elements from begin to begin+2 (2 elements) in a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3, 4, 5};
|
||||
d.erase(d.begin(), d.begin() + 2); // {3, 4, 5}
|
||||
#+end_src
|
||||
|
||||
* Task: swap two deques :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to swap the contents of two deques
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d1 = {1, 2, 3};
|
||||
std::deque<int> d2 = {4, 5, 6};
|
||||
d1.swap(d2); // d1 = {4,5,6}, d2 = {1,2,3}
|
||||
#+end_src
|
||||
|
||||
* Task: emplace at specific position :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to construct a pair in-place at position begin of a deque
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<std::pair<int,int>> d;
|
||||
d.emplace(d.begin(), 1, 2); // constructs pair{1,2} at begin
|
||||
#+end_src
|
||||
|
||||
* Task: assign new contents to deque :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to replace all contents of a deque with 5 copies of 99
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d = {1, 2, 3};
|
||||
d.assign(5, 99); // {99, 99, 99, 99, 99}
|
||||
#+end_src
|
||||
|
||||
* Task: assign from range :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to replace deque contents with all elements from a vector
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> d;
|
||||
std::vector<int> v = {10, 20, 30};
|
||||
d.assign(v.begin(), v.end()); // {10, 20, 30}
|
||||
#+end_src
|
||||
|
||||
* Task: use deque as FIFO queue :cpp:deque:production:
|
||||
:PROPERTIES:
|
||||
:ANKI_NOTE_TYPE: Basic
|
||||
:END:
|
||||
** Front
|
||||
Write how to use deque as a FIFO queue (enqueue 1, 2, 3 then dequeue all)
|
||||
** Back
|
||||
#+begin_src c++
|
||||
std::deque<int> q;
|
||||
q.push_back(1); // enqueue
|
||||
q.push_back(2);
|
||||
q.push_back(3);
|
||||
|
||||
while (!q.empty()) {
|
||||
std::cout << q.front() << " ";
|
||||
q.pop_front(); // dequeue
|
||||
}
|
||||
// Output: 1 2 3
|
||||
#+end_src
|
||||
Reference in New Issue
Block a user