今天学了Dynamic Array Stack,教授把模板代码上传到网上。如果您有兴趣查看http://ideone.com/oXe2t1 ,请点击此处的链接。但是代码中有一些我不明白的部分。
// I did not know how he comes up with the (3 * _size)
void pop() {
assert(!is_empty());
_size--;
if (_capacity > (3 * _size))
resize();
}
然后在 resize()
//how does he know that the max capacity will be equal to either (_size * 2)
// or DYNAMIC_ARRAYED_STACK_MIN_CAPACITY
void resize() {
_capacity = max(_size * 2, DYNAMIC_ARRAYED_STACK_MIN_CAPACITY);
unique_ptr<E[]> new_array(new E[_capacity]);
for (int i = 0; i < _size; i++)
new_array[i] = _elements[i];
_elements.swap(new_array);
}