Практика 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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