Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by k3l on 21.11.2019.
- //
- #include "my_vector.h"
- #include <exception>
- #include <iostream>
- namespace my_std{
- int min(int i, int j){
- return i < j ? i : j;
- }
- my_vector::my_vector() {
- this->size = 0;
- this->alloc = 0;
- this->array = nullptr;
- }
- my_vector::my_vector(int n) {
- this->size = 0;
- this->alloc = n;
- this->array = new int[n];
- }
- my_vector::my_vector(int n, int x) {
- this->size = n;
- this->alloc = n;
- this->array = new int[n];
- for(int i = 0; i < n; i++){
- array[i] = x;
- }
- }
- void my_vector::resize(int new_size) {
- int *temp = new int[new_size];
- for(int i = 0; i < min(this->size,new_size); i++){
- temp[i] = this->array[i];
- }
- delete[] array;
- this->array = temp;
- this->size = min(size, new_size);
- this->alloc = new_size;
- }
- int& my_vector::operator[](int index) {
- if(index < size)
- return array[index];
- throw std::exception();
- }
- int my_vector::get_size() {
- return size;
- }
- int my_vector::pop() {
- return array[--size];
- }
- void my_vector::push_back(int i) {
- if(alloc == size)
- resize((int) (alloc * 1.5));
- array[size++] = i;
- }
- my_vector::~my_vector() {
- delete[] array;
- std::cout << "I've been deleted" << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment