You cant use just initializer lists to get it done. Use std::fill_n
like this,
class Vector{
private:
int size_;
int* array_;
public:
Vector(int length, int value) : size_(length), array_(new int[length * 2])
{
std::fill_n(array_, length, 0);
}
}
Note: Using array_(new int[length * 2])
might not be the best way to allocate the buffer. For example, if the user wants to store just 100 integers, you actually allocate 200 entries worth of memory. This might be a good idea for performance but might not be for memory. Another thing is that your vector only supports integers. If you want to support other types as well, make it a templated class.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…