ProblemsModern C++A queue that speaks in operators
IntermediateModern C++

A queue that speaks in operators

Context

A queue can expose its operations through operators too. The interesting pair is the decrements: postfix and prefix drop from OPPOSITE ends here, so one cannot be implemented through the other. And x + q with the int on the left is a free function again.

Task

Implement Queue: q + x enqueues at the back, x + q pushes to the front, q-- drops the BACK element, --q drops the FRONT one. front()/size() expose state for the checks.

Constraints

  • q + x adds to the back; x + q adds to the front
  • q-- removes the back element; --q removes the front
  • front() and size() expose state; operations on an empty queue are not tested
  • Order of everything else is preserved

Before you code

  • What signature difference distinguishes prefix from postfix operator--?
  • Why can q-- not be expressed through --q here?
  • Which std adapter is closest to this Queue, and what does it forbid?

Tests

  • #1The full operator sequence
  • #2Front and back drops are not the same

Hints

Hint 1

operator--(int) is the postfix form — the dummy int only selects the overload.

Hint 2

std::deque underneath gives both pop_front and pop_back for free.

Editorqueue-operators.cpp
Results

Hit Submit (or ⌘/Ctrl + ↵) — test results will show up here.