12790 - Copy a vector again   

Description

You must follow the rules as below:

 

1. do not use any static variables

2. do not use any variables which is not inside a function

3. only new, operator new, delete and operator delete are allowed to allocate or deallocate memory

4. memory usage limit: 16384 byte

You are required to implement a vector.

You have to enable C++11 in this problem. (e.g. "g++ -std=c++11 ...")

 

Input

There are seven functions in Vector that you have to implement, the functionality is as below:

  • default constructor: initializes pointers. Do not allocate memory.
  • copy constructor: copies the elements of rhs. The new capacity is rhs.size().
  • copy assignment operator: copies the elements of rhs. The new capacity is max(capacity(), rhs.size()).
  • erase: erases the first element that is equal to val (if it exists).
  • insert: inserts a val at pos. When capacity is not enough, the new capacity is max(old capacity + 1, old capacity * 3).
  • reserve: reserves storage (Increase the capacity of the container to a value that's equal to new capacity. If the new capacity is greater than the current capacity, new storage is allocated, otherwise the function does nothing.).
  • destructor

 

For all testcases, you have to deal with reserve() and whatever is not explicitly mentioned below;
For the first testcase, you only need to deal with insert();
For the second and third, you only need insert() and erase();
For the fourth, you may only omit copy assignment;
For all, you need to do all.

Output

Complete a Vector (default constructor, copy constructor, copy assignment operator, erase, insert, reserve and destructor).

Sample Input  Download

Sample Output  Download

Partial Judge Code

12790.cpp

Partial Judge Header

12790.h

Tags




Discuss