Files
2026-05-04 08:12:20 +08:00

9.2 KiB

Task: construct an empty deque   cpp deque production

Front

Write how to create an empty deque of ints

Back

std::deque<int> d;

Task: construct deque with 5 copies of value   cpp deque production

Front

Write how to create a deque with 5 elements, all initialized to 42

Back

std::deque<int> d(5, 42);  // {42, 42, 42, 42, 42}

Task: construct deque from initializer list   cpp deque production

Front

Write how to create a deque initialized with {1, 2, 3}

Back

std::deque<int> d = {1, 2, 3};

Task: construct deque from a range   cpp deque production

Front

Write how to create a deque from a vector's begin/end iterators

Back

std::vector<int> v = {1, 2, 3, 4, 5};
std::deque<int> d(v.begin(), v.end());

Task: access element at index with bounds checking   cpp deque production

Front

Write how to safely access element at index 2 in a deque (throws on invalid index)

Back

std::deque<int> d = {10, 20, 30};
int x = d.at(2);  // returns 30, throws std::out_of_range if invalid

Task: access element at index with operator[]   cpp deque production

Front

Write how to access element at index 1 using operator[]

Back

std::deque<int> d = {10, 20, 30};
int x = d[1];  // returns 20, no bounds check

Task: access first and last elements   cpp deque production

Front

Write how to get the first and last element of a deque

Back

std::deque<int> d = {10, 20, 30};
int first = d.front();  // 10
int last = d.back();    // 30

Task: iterate over deque with begin/end   cpp deque production

Front

Write a range-based for loop over a deque using iterators

Back

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 << " "; }

Task: reverse iterate over deque   cpp deque production

Front

Write how to iterate backwards through a deque using reverse iterators

Back

std::deque<int> d = {1, 2, 3};
for (auto it = d.rbegin(); it != d.rend(); ++it) {
    std::cout << *it << " ";  // prints 3 2 1
}

Task: check if deque is empty   cpp deque production

Front

Write how to check if a deque is empty

Back

std::deque<int> d;
if (d.empty()) { /* ... */ }

Task: get size of deque   cpp deque production

Front

Write how to get the number of elements in a deque

Back

std::deque<int> d = {1, 2, 3};
std::size_t n = d.size();  // returns 3

Task: clear all elements from deque   cpp deque production

Front

Write how to remove all elements from a deque

Back

std::deque<int> d = {1, 2, 3};
d.clear();  // size becomes 0

Task: push elements to back of deque   cpp deque production

Front

Write how to add elements 10, 20, 30 to the back of a deque

Back

std::deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_back(30);

Task: push elements to front of deque   cpp deque production

Front

Write how to add elements 30, 20, 10 to the front of a deque (pushing in that order)

Back

std::deque<int> d;
d.push_front(30);  // {30}
d.push_front(20);  // {20, 30}
d.push_front(10);  // {10, 20, 30}

Task: pop elements from back of deque   cpp deque production

Front

Write how to remove the last element from a deque

Back

std::deque<int> d = {1, 2, 3};
d.pop_back();  // {1, 2}

Task: pop elements from front of deque   cpp deque production

Front

Write how to remove the first element from a deque

Back

std::deque<int> d = {1, 2, 3};
d.pop_front();  // {2, 3}

Task: emplace back into deque   cpp deque production

Front

Write how to construct a pair in-place at the back of a deque (without copying)

Back

std::deque<std::pair<int,int>> d;
d.emplace_back(1, 2);  // constructs pair{1,2} directly

Task: emplace front into deque   cpp deque production

Front

Write how to construct an element in-place at the front of a deque

Back

std::deque<std::string> d;
d.emplace_front("hello");  // constructs string directly at front

Task: resize deque   cpp deque production

Front

Write how to resize a deque to 10 elements, filling new spots with 0

Back

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

Task: insert element at position   cpp deque production

Front

Write how to insert value 99 at the beginning of a deque

Back

std::deque<int> d = {1, 2, 3};
d.insert(d.begin(), 99);  // {99, 1, 2, 3}

Task: insert multiple copies   cpp deque production

Front

Write how to insert 5 copies of value 42 starting at position begin

Back

std::deque<int> d = {1, 2, 3};
d.insert(d.begin(), 5, 42);  // {42, 42, 42, 42, 42, 1, 2, 3}

Task: insert range into deque   cpp deque production

Front

Write how to insert all elements from a vector into a deque at position begin

Back

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}

Task: erase single element   cpp deque production

Front

Write how to erase the element at position begin + 1 in a deque

Back

std::deque<int> d = {1, 2, 3};
d.erase(d.begin() + 1);  // {1, 3}

Task: erase range of elements   cpp deque production

Front

Write how to erase elements from begin to begin+2 (2 elements) in a deque

Back

std::deque<int> d = {1, 2, 3, 4, 5};
d.erase(d.begin(), d.begin() + 2);  // {3, 4, 5}

Task: swap two deques   cpp deque production

Front

Write how to swap the contents of two deques

Back

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}

Task: emplace at specific position   cpp deque production

Front

Write how to construct a pair in-place at position begin of a deque

Back

std::deque<std::pair<int,int>> d;
d.emplace(d.begin(), 1, 2);  // constructs pair{1,2} at begin

Task: assign new contents to deque   cpp deque production

Front

Write how to replace all contents of a deque with 5 copies of 99

Back

std::deque<int> d = {1, 2, 3};
d.assign(5, 99);  // {99, 99, 99, 99, 99}

Task: assign from range   cpp deque production

Front

Write how to replace deque contents with all elements from a vector

Back

std::deque<int> d;
std::vector<int> v = {10, 20, 30};
d.assign(v.begin(), v.end());  // {10, 20, 30}

Task: use deque as FIFO queue   cpp deque production

Front

Write how to use deque as a FIFO queue (enqueue 1, 2, 3 then dequeue all)

Back

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