在申请过程中,我被设置为一项家庭作业挑战(顺便说一句,我被拒绝了;否则我不会写这个),其中我将实现以下功能:
// Store a collection of integers
class IntegerCollection {
public:
// Insert one entry with value x
void Insert(int x);
// Erase one entry with value x, if one exists
void Erase(int x);
// Erase all entries, x, from <= x < to
void Erase(int from, int to);
// Return the count of all entries, x, from <= x < to
size_t Count(int from, int to) const;
Run Code Online (Sandbox Code Playgroud)
然后对功能进行了一系列测试,其中大多数测试都是微不足道的。最终测试是真正的挑战,因为它执行了500,000次单次插入,500,000次呼叫计数和500,000次单次删除。
的成员变量IntegerCollection
未指定,因此我必须选择如何存储整数。自然地,一个STL容器似乎是一个好主意,并且对其进行排序似乎是使事情保持高效的简单方法。
这是我使用a的四个函数的代码vector
:
// Store a collection of …
Run Code Online (Sandbox Code Playgroud)