#include <iostream>
struct MyArray
{
using value_type = double; // make value_type an alias for double.
explicit MyArray(unsigned size=64)
: _storage(new value_type[size]), _capacity(size), _used(0) {}
~MyArray() { delete [] _storage; }
unsigned capacity() const { return _capacity; }
unsigned size() const { return _used; }
void add_value(value_type value);
friend std::ostream& operator <<(std::ostream& out, const MyArray& arr);
private:
value_type* _storage;
unsigned _capacity;
unsigned _used;
};
void MyArray::add_value(MyArray::value_type value)
{
if (size() == capacity())
{
// grow the array.
throw; // until functionality is implemented.
}
_storage[_used++] = value;
}
// display a MyArray:
std::ostream& operator <<(std::ostream& out, const MyArray& arr)
{
out << "{ ";
if (arr.size())
{
unsigned i = 0;
while (i < arr.size() - 1)
out << arr._storage[i++] << ", ";
out << arr._storage[i] << ' ';
}
return out << '}' ;
}
int main()
{
MyArray arr(3);
std::cout << arr << "\n\n";
arr.add_value(1.1);
arr.add_value(2.2);
arr.add_value(3.3);
std::cout << arr << '\n';
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RydWN0IE15QXJyYXkKewogICAgdXNpbmcgdmFsdWVfdHlwZSA9IGRvdWJsZTsgIC8vIG1ha2UgdmFsdWVfdHlwZSBhbiBhbGlhcyBmb3IgZG91YmxlLgoKICAgIGV4cGxpY2l0IE15QXJyYXkodW5zaWduZWQgc2l6ZT02NCkgCiAgICAgICAgOiBfc3RvcmFnZShuZXcgdmFsdWVfdHlwZVtzaXplXSksIF9jYXBhY2l0eShzaXplKSwgX3VzZWQoMCkge30KCiAgICB+TXlBcnJheSgpIHsgZGVsZXRlIFtdIF9zdG9yYWdlOyB9CgogICAgdW5zaWduZWQgY2FwYWNpdHkoKSBjb25zdCB7IHJldHVybiBfY2FwYWNpdHk7IH0KICAgIHVuc2lnbmVkIHNpemUoKSBjb25zdCB7IHJldHVybiBfdXNlZDsgfQoKICAgIHZvaWQgYWRkX3ZhbHVlKHZhbHVlX3R5cGUgdmFsdWUpOwoKICAgIGZyaWVuZCBzdGQ6Om9zdHJlYW0mIG9wZX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