Skip to content

Instantly share code, notes, and snippets.

@vasalf
Created November 8, 2019 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasalf/d687d73f7e2a165db86a518eb6356788 to your computer and use it in GitHub Desktop.
Save vasalf/d687d73f7e2a165db86a518eb6356788 to your computer and use it in GitHub Desktop.
Практика 8
#include "vector.h"
vector_int::vector_int() {
size_ = 0;
capacity_ = 16;
data_ = new int[capacity_];
}
void vector_int::reserve(size_t new_size) {
if (new_size <= capacity_)
return;
capacity_ = new_size;
int *new_data = new int[capacity_];
for (size_t i = 0; i != size_; i++) {
new_data[i] = data_[i];
}
delete[] data_;
data_ = new_data;
}
#pragma once
#include <cstddef>
class vector_int {
public:
vector_int();
~vector_int();
vector_int(const vector_int& other);
vector_int& operator=(const vector_int& other);
void push_back(int x);
void reserve(size_t new_size);
int& operator[](size_t id);
const int& operator[](size_t id) const;
private:
int *data_;
size_t size_;
size_t capacity_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment