14288 - Queue with Random Access   

Description

In this problem, you are asked to implement some functions of a queue with random access.

You are asked to implement a class IQueue with the following functions:

  1. IQueue::IQueue(): Default constructor. Already implemented.
  2. IQueue::IQueue(IQueue const& rhs): Copy constructor. Construct a new queue with the same elements as rhs.
  3. IQueue::IQueue(IQueue&& rhs): Move constructor. Construct a new queue by moving the elements from rhs.
  4. IQueue& IQueue::operator=(IQueue const& rhs): Copy assignment operator. Assign the elements of rhs to the current queue.
  5. IQueue& IQueue::operator=(IQueue&& rhs): Move assignment operator. Move the elements from rhs to the current queue.
  6. IQueue::~IQueue(): Destructor.
  7. void IQueue::Push(int val): Push val to the back of the queue.
  8. void IQueue::Pop(void): Pop the front element of the queue.
  9. int& IQueue::Front(void): Return a reference to the front element of the queue.
  10. int& IQueue::operator[](size_t pos): Return a reference to the element at index pos of the queue. It is guaranteed that pos is in the range \([0, size)\), so you don't need to check for out of range errors.
  11. int& IQueue::At(size_t pos): Return a reference to the element at index pos of the queue. If pos is out of range, throw an std::out_of_range exception with the message "Out of range".
  12. void IQueue::Swap(IQueue& rhs): Swap the elements of the current queue with rhs.
  13. bool IQueue::Empty(void): Return true if the queue is empty, false otherwise.
  14. size_t IQueue::Size(void): Return the number of elements in the queue. Already implemented.
  15. void IQueue::DoubleCapacity(void): Double the capacity of the queue.

Note: The capacity of the queue is the maximum number of elements that can be stored in data. The size of the queue is the number of elements currently in data. When Push is called and the size of the queue is equal to the capacity, you should double the capacity of the queue.

Warning: You should use C++11 or later versions, or else you will get Compile Error! The move constructor and move assignment operator are added in C++11.

Hints:
What is an lvalue?
Value categories
Move Constructors in C++ with Examples
std::move

Input

This is a partial judge problem, input and output are handled by main.cpp.

Constraints

  • \(10 \leq N \leq 10^5\) (\(N\) is the number of commands)
  • For IQueue::Pop(void) and IQueue::Front(void), the queue is not empty.
  • For IQueue::operator[](int pos), the index pos is in the range \([0, size)\).

Output

This is a partial judge problem, input and output are handled by main.cpp.

Sample Input  Download

Sample Output  Download

Partial Judge Code

14288.cpp

Partial Judge Header

14288.h


Discuss