Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename T>
- class Vector {
- public:
- Vector() : size_(0), capacity_(4), arr_(new T[4]) {}
- explicit Vector(const int vector_capacity) : size_(0), capacity_(vector_capacity), arr_(new T[vector_capacity]) {}
- Vector(const Vector& other) : size_(other.size_), capacity_(other.capacity_), arr_(new T[capacity_]) {
- for (int i = 0; i < size_; i++) {
- arr_[i] = other.arr_[i];
- }
- total_number_elements_ += size_;
- } // конструктор копирования
- Vector(const initializer_list<T>& list) : size_(list.size()), capacity_(list.size()), arr_(new T[list.size()]) {
- int i = 0;
- for (const T& element : list) {
- arr_[i] = element;
- i++;
- }
- }
- Vector& operator=(const Vector& other) {
- if (&other != this) { // проверка на самоприсваивание
- total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
- size_ = other.size_;
- capacity_ = other.capacity_;
- delete[] arr_;
- arr_ = new T[capacity_];
- for (int i = 0; i < size_; i++) {
- arr_[i] = other.arr_[i];
- }
- }
- return *this;
- } // копирующий оператор присваивания =
- Vector(Vector&& other) : size_(other.size_), capacity_(other.capacity_), arr_(other.arr_) {
- other.arr_ = nullptr;
- other.size_ = 0;
- other.capacity_ = 0;
- } // конструктор перемещения
- Vector& operator=(Vector&& other) {
- if (&other != this) {
- total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
- size_ = other.size_;
- capacity_ = other.capacity_;
- delete[] arr_;
- arr_ = other.arr_;
- other.arr_ = nullptr;
- other.size_ = 0;
- other.capacity_ = 0;
- }
- return *this;
- } // перемещающий оператор присваивания =
- void push_back(const T& value) {
- check_capacity();
- arr_[size_] = value;
- size_++;
- total_number_elements_++;
- }
- void pop_back() {
- if (size_ == 0) {
- throw runtime_error("Попытка удаления в пустом контейнере");
- }
- size_--;
- total_number_elements_--;
- }
- void insert(const int index, const T& value) { // типы ошибок можно разделить
- if (index < 0 || index >= size_) {
- throw logic_error("Некорректный индекс");
- }
- check_capacity();
- for (int i = size_; i > index; i--) {
- arr_[i] = arr_[i - 1];
- }
- arr_[index] = value;
- size_++;
- total_number_elements_++;
- }
- void erase(const int index) {
- if (index < 0 || index >= size_) {
- throw logic_error("Некорректный индекс");
- }
- for (int i = index; i < size_ - 1; i++) {
- arr_[i] = arr_[i + 1];
- }
- size_--;
- total_number_elements_--;
- }
- T& operator[](const int index) {
- if (index < 0 || index >= size_) {
- throw logic_error("Некорректный индекс");
- }
- return arr_[index];
- }
- const T& operator[](const int index) const {
- if (index < 0 || index >= size_) {
- throw logic_error("Некорректный индекс");
- }
- return arr_[index];
- }
- int size() const {
- return size_;
- }
- int capacity() const {
- return capacity_;
- }
- bool operator==(const Vector& right) const {
- if (right.size_ != size_) {
- return false;
- }
- for (int i = 0; i < size_; i++) {
- if (arr_[i] != right.arr_[i])
- return false;
- }
- return true;
- }
- bool operator!=(const Vector& right) const {
- return !(*this == right);
- }
- void print() const {
- for (int i = 0; i < size_; i++) {
- cout << arr_[i] << " ";
- }
- cout << endl;
- }
- static int get_total_number_elements() {
- return total_number_elements_;
- }
- ~Vector() {
- delete[] arr_;
- total_number_elements_ -= size_;
- }
- private:
- void check_capacity() {
- if (size_ == capacity_) {
- int* temp = new int[capacity_ * 2];
- for (int i = 0; i < size_; i++) {
- temp[i] = arr_[i];
- }
- delete[] arr_;
- arr_ = temp;
- capacity_ *= 2;
- }
- }
- int size_; // текущее количество элементов
- int capacity_; // емкость хранилища
- T* arr_; // хранилище
- static int total_number_elements_; // общее количество элементов по всем векторам
- };
- template <typename T>
- int Vector<T>::total_number_elements_ = 0;
- int main() {
- setlocale(LC_ALL, "ru");
- try {
- Vector<int> vec{ 1,2,3,4,5 };
- vec.print();
- // vec.insert(9, 12);
- vec.print();
- //for (int i = 0; i < 7; i++) {
- // vec.pop_back();
- //}
- //for (int i = 0; i < 10000000000; i++) {
- // vec.push_back(i);
- //}
- }
- catch (const runtime_error& ex) {
- cout << ex.what() << " 1 " << endl;
- }
- catch(const logic_error& ex) {
- cout << ex.what() << " 2 " << endl;
- }
- catch (const bad_alloc& ex) {
- cout << ex.what() << " 3 " << endl;
- }
- catch (...) { // ловим ошибки любого другого типа
- cout << "Другая ошибка" << " 4 " << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement