# 13520 - Dynamic Array >author: Utin ###### tags: `class` --- ## Brief See the code below ## Solution 0 ```cpp= #include <iostream> #include "function.h" Darray::~Darray() { delete[] data; } int& Darray::operator[](int idx) { return data[idx]; } void Darray::pushback(int x) { if (size >= capacity) resize(); data[size] = x; size++; } void Darray::clear(void) { delete[] data; data = new int[capacity]; size = 0; } int Darray::length(void) { return size; } void Darray::resize(void) { capacity *= 2; int* tmp = new int[capacity]; // copy data for (int i = 0; i < size; i++) tmp[i] = data[i]; // free memory delete[] data; data = tmp; } // Utin ``` ## Reference
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up