Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <algorithm>
- #include <iostream>
- #include <cassert>
- using namespace std;
- class Vector {
- public:
- int my_size;
- int my_capacity;
- int* buffer;
- Vector() {
- my_capacity = 0;
- my_size = 0;
- buffer = 0;
- }
- Vector(const Vector & v) {
- my_size = v.my_size;
- my_capacity = v.my_capacity;
- buffer = new int[my_size];
- for (int i = 0; i < my_size; i++)
- buffer[i] = v.buffer[i];
- }
- Vector(unsigned int size) {
- my_capacity = size;
- my_size = size;
- buffer = new int[size];
- }
- Vector(unsigned int size, const int& initial) {
- my_size;
- my_capacity = size;
- buffer = new int[size];
- for (int i = 0; i < size; i++)
- buffer[i] = initial;
- }
- int & operator[](int index) {
- return buffer[index];
- }
- Vector& operator = (const Vector & v) {
- delete[] buffer;
- my_size = v.my_size;
- my_capacity = v.my_capacity;
- buffer = new int[my_size];
- for (int i = 0; i < my_size; i++)
- buffer[i] = v.buffer[i];
- return *this;
- }
- void reserve(int capacity) {
- if (buffer == 0) {
- my_size = 0;
- my_capacity = 0;
- }
- if (capacity <= my_capacity)
- return;
- int* new_buffer = new int[capacity];
- assert(new_buffer);
- copy(buffer, buffer + my_size, new_buffer);
- my_capacity = capacity;
- delete[] buffer;
- buffer = new_buffer;
- }
- void push_back(const int & v) {
- if (my_size >= my_capacity) {
- float k = (1 + sqrt(5) / 2);
- reserve(static_cast <int>(k * (my_size + 1)));
- }
- buffer[my_size++] = v;
- }
- void pop_back() {
- my_size--;
- }
- int size() const {
- return my_size;
- }
- ~Vector() {
- delete[] buffer;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement