| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11410 | Implement a vector 2 |
|
| 11419 | Using your vector (Implement a vector 3) |
|
Description
Warning: You are not allowed to use:
1. any static variables
2. any variables which is not inside a function
3. malloc and free
由於實際的code有做其他檢查,因此為了讓各位方便閱讀,請參考簡化版本的function.h與main.cpp(請點連結)
Vectors are sequence containers representing arrays that can change in size.
The storage of the vector is handled automatically, being expanded and contracted as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted.
Note:
If the value of size is equal to the value of capacity, and you need to change the value of capacity (reallocate memory) when you push_back a new element. The rule of increasing capacity is: new capacity = max(old capacity + 1, old capacity * 3).
The constructor of vector will not create an array (which means size and capacity is 0).
Input
There are seven kinds of commands:
- pop_back: removes the last element
- push_back: adds an element to the end
- capacity: returns the number of elements that can be held in currently allocated storage
- size: returns the number of elements
- insert: inserts elements (If there is no capacity for new elements, the new capacity is max (old capacity + old capacity / 2, old capacity + count of inserting elements).) (If a vector has two elements, inserting three elements at position two means push back three times.)
- reserve: reserves storage (Increase the capacity of the container to a value that's equal to new capacity. If new capacity is greater than the current capacity, new storage is allocated, otherwise the method does nothing.)
- resize: changes the number of elements stored (If the value of new size is greater than the value of old size, the value of new elements will be 0. If the value of new size is greater than the value of old capacity, the value of new capacity will be new size.)
Each commands is followed by a new line character ('\n').
Output
Implement Vector (constructor, push_back(), pop_back(), capacity(), size(), reserve(), resize(), insert() and destructor).
Sample Input Download
Sample Output Download
Partial Judge Code
11410.cppPartial Judge Header
11410.hTags
Discuss
Description
Warning: You are not allowed to use:
1. any static variables
2. any variables which is not inside a function
3. malloc and free
由於實際的code有做其他檢查,因此為了讓各位方便閱讀,請參考簡化版本的function.h與main.cpp(請點連結)
This problem is similar to 11410 - Implement vector, but you have to do more things.
About how the instructions of vector work, please check above link.
This vector has an another operation, which is
- erase(): erases elements
You are required to do an employee statistic. Every employees has a name and an unique ID (starts from 0, increment after recruiting an employee).
You will have a class "God" which helps you construct class Employee and read the data members of Employee.
You have to implement
- Vector (push_back, erase, reserve, destructor)
- Employee(const std::string &name) (Don't forget to increase the ID_.)
- void add_employee(Vector &vec,const std::string &name)
- void print(const Vector &vec);
- void quit(Vector &vec,unsigned id)
- void quit(Vector &vec,const std::string &name)
You have to enable C++11 in this problem. (e.g. "g++ -std=c++11 ...")
Hint:
In C++, besides new expression, there is two operations simliar to new expression. One is called "operator new" (allocate memory) and the other is called "placement new" (construct object). You can think "new expression = operator new + placement new".
Here is some examples,
Input
Each input lines may be "recruit [name]", "quit 0 [name]", "quit 1 [id]", "reserve [size]", "capacity" or "size".
"recruit [name]" means push back the employee's [name] to the vector.
"quit 0 [name]" means erase employees from the vector if any employee's name is equal to the [name].
"quit 1 [id]" means erase employees from the vector if any employee's id is equal to the [id].
"reserve [size]" means reserve storage. (Increase the capacity of the container to a value that's greater or equal to new capacity. If new capacity is greater than the current capacity, new storage is allocated, otherwise the method does nothing.)
"capacity" means return the number of elements that can be held in currently allocated storage.
"size" means return the number of elements.
Output
Do the above operations. Print all elements in the vec after doing any operations. The print formate should be "employee_id employee_name\n".