Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class vector {
- private:
- int* data;
- int size;
- int capacity;
- void resize() {
- int newCapacity = 2 * capacity;
- int* newData = new int[newCapacity];
- for (int i = 0; i < capacity; i++)
- newData[i] = data[i];
- delete[] data;
- data = newData;
- capacity = newCapacity;
- }
- void copy(const vector& other) {
- data = new int[other.getCapacity()];
- for (int i = 0; i < other.size; i++)
- data[i] = other.data[i];
- size = other.size;
- capacity = other.capacity;
- }
- void free() {
- delete[] data;
- }
- public:
- vector() : data(new int[8]), size(0), capacity(8) {}
- vector(const vector& other) {
- copy(other);
- }
- vector& operator=(const vector& other) {
- if (this != &other) {
- free();
- copy(other);
- }
- return *this;
- }
- int getSize() const {
- return size;
- }
- int getCapacity() const {
- return capacity;
- }
- void push_back(int& val) {
- if (size == capacity)
- resize();
- data[size++] = val;
- }
- int operator[](int arg) const {
- if (arg < 0 || arg >= size)
- throw std::exception("out of bound");
- return data[arg];
- }
- ~vector() {
- free();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement