Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <initializer_list>
- #include <iostream>
- using namespace std;
- template <typename T>
- class Vector {
- public:
- Vector() : size(0), capacity(4), arr(new T[4]) {}
- Vector(const int vector_capacity) : size(0), capacity(vector_capacity), arr(new T[vector_capacity]) {}
- Vector(const initializer_list<T>& list) : arr(new T[list.size()]{}), size(list.size()), capacity(list.size()) {
- int i = 0;
- for (auto element : list) {
- arr[i] = element;
- i++;
- }
- }
- 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& operator=(const Vector& other) {
- if (&other != this) {
- total_number_elements -= 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];
- }
- total_number_elements += size;
- }
- 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 -= size;
- size = other.size;
- capacity = other.capacity;
- delete[] arr;
- arr = other.arr;
- other.arr = nullptr;
- other.size = 0;
- other.capacity = 0;
- total_number_elements += size;
- }
- return *this;
- } // перемещающий оператор присваивания =
- T& operator[] (const int index) {
- if (index < 0 || index >= size) {
- throw logic_error("Некорректный индекс");
- }
- return arr[index];
- } // перегрузка оператора индексирования (сеттер)
- T operator[] (const int index) const {
- if (index < 0 || index >= size) {
- throw logic_error("Некорректный индекс");
- }
- return arr[index];
- } // перегрузка оператора индексирования для константных объектов
- 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 Push_back(const T value) {
- if (size == capacity) {
- T* temp = new T[capacity * 2];
- for (int i = 0; i < size; i++) {
- temp[i] = arr[i];
- }
- delete[] arr;
- arr = temp;
- capacity *= 2;
- }
- arr[size] = value;
- size++;
- total_number_elements++;
- }
- void Pop_back() {
- if (size == 0) {
- throw runtime_error("Пустой контейнер");
- }
- arr[size - 1] = 0;
- size--;
- total_number_elements--;
- }
- int Size() {
- return size;
- }
- int Capacity() {
- return capacity;
- }
- void Insert(const int index, const T value) {
- if (index < 0 || index >= size) {
- throw logic_error("Некорректный индекс");
- }
- if (size == capacity) {
- T* temp = new T[capacity * 2];
- for (int i = 0; i < size; i++) {
- temp[i] = arr[i];
- }
- delete[] arr;
- arr = temp;
- capacity *= 2;
- }
- 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];
- }
- arr[size - 1] = 0;
- size--;
- total_number_elements--;
- }
- T GetValue(const int index) {
- if (index < 0 || index >= size) {
- throw logic_error("Некорректный индекс");
- }
- return arr[index];
- }
- void SetValue(const int index, const T value) {
- if (index < 0 || index >= size) {
- throw logic_error("Некорректный индекс");
- }
- arr[index] = value;
- }
- static int GetTotalNumberElements() {
- return total_number_elements;
- }
- void Print() {
- for (int i = 0; i < size; i++) {
- cout << arr[i] << " ";
- }
- cout << endl;
- }
- ~Vector() {
- delete[] arr;
- total_number_elements -= size;
- }
- private:
- int size;
- int capacity;
- T* arr;
- static int total_number_elements;
- };
- int Vector<int>::total_number_elements = 0;
- int Vector<string>::total_number_elements = 0;
- template<typename T>
- class List {
- public:
- List() : size(0), head(nullptr), last(nullptr) {}
- List(const initializer_list<T>& list) : size(0), head(nullptr), last(nullptr) {
- for (const auto& element : list) {
- Push_back(element);
- }
- }
- List(const List& other) : size(other.size), head(nullptr), last(nullptr) {
- if (other.head != nullptr) {
- head = new Node{ other.head->value, nullptr, nullptr };
- Node* temp = head;
- Node* other_temp = other.head;
- while (other_temp->next != nullptr) {
- other_temp = other_temp->next;
- temp->next = new Node{ other_temp->value, nullptr, temp };
- temp = temp->next;
- }
- last = temp;
- }
- }
- List(List&& other) : size(other.size), head(other.head), last(other.last) {
- other.size = 0;
- other.head = nullptr;
- other.last = nullptr;
- }
- List& operator=(const List& other) {
- if (this != &other) {
- Clear();
- if (other.head != nullptr) {
- head = new Node{ other.head->value, nullptr, nullptr };
- Node* temp = head;
- Node* other_temp = other.head;
- while (other_temp->next != nullptr) {
- other_temp = other_temp->next;
- temp->next = new Node{ other_temp->value, nullptr, temp };
- temp = temp->next;
- }
- last = temp;
- size = other.size;
- }
- }
- return *this;
- }
- List& operator=(List&& other) {
- if (this != &other) {
- Clear();
- size = other.size;
- head = other.head;
- last = other.last;
- other.size = 0;
- other.head = nullptr;
- other.last = nullptr;
- }
- return *this;
- }
- void Push_front(const T& value) {
- if (size == 0) {
- last = head = new Node{ value, nullptr, nullptr };
- size++;
- return;
- }
- Node* temp = new Node{ value, head, nullptr };
- head->prev = temp;
- head = temp;
- size++;
- }
- void Push_back(const T& value) {
- if (size == 0) {
- last = head = new Node{ value, nullptr, nullptr };
- size++;
- return;
- }
- Node* temp = new Node{ value, nullptr, last };
- last->next = temp;
- last = temp;
- size++;
- }
- void Pop_front() {
- if (size == 0) {
- throw runtime_error("Попытка удаления в пустом контейнере");
- }
- if (size == 1) {
- delete head;
- last = head = nullptr;
- size--;
- return;
- }
- head = head->next;
- delete head->prev;
- head->prev = nullptr;
- size--;
- }
- void Pop_back() {
- if (size == 0) {
- throw runtime_error("Попытка удаления в пустом контейнере");
- }
- if (size == 1) {
- delete head;
- last = head = nullptr;
- size--;
- return;
- }
- last = last->prev;
- delete last->next;
- last->next = nullptr;
- size--;
- }
- void Print() const {
- Node* temp = head;
- while (temp != nullptr) {
- cout << temp->value << " ";
- temp = temp->next;
- }
- cout << endl;
- }
- T& Front() {
- if (head == nullptr) {
- throw runtime_error("Обращение к элементу пустого контейнера");
- }
- return head->value;
- }
- const T& Front() const {
- if (head == nullptr) {
- throw runtime_error("Обращение к элементу пустого контейнера");
- }
- return head->value;
- }
- T& Back() {
- if (last == nullptr) {
- throw runtime_error("Обращение к элементу пустого контейнера");
- }
- return last->value;
- }
- const T& Back() const {
- if (last == nullptr) {
- throw runtime_error("Обращение к элементу пустого контейнера");
- }
- return last->value;
- }
- int Size() const {
- return size;
- }
- void Clear() {
- while (head != nullptr) {
- Pop_front();
- }
- }
- void Insert(const int index, const int& value) {
- if (index < 0 || index > size) {
- throw logic_error("Невалидный индекс");
- }
- if (index == 0) {
- Push_front(value);
- return;
- }
- if (index == size) {
- Push_back(value);
- return;
- }
- if (index > 0 && index < size) {
- Node* temp = head;
- for (int i = 0; i < index - 1; i++) {
- temp = temp->next;
- }
- Node* node = new Node{ value, temp->next, temp };
- temp->next->prev = node;
- temp->next = node;
- size++;
- }
- }
- void Erase(const int index) {
- if (index < 0 || index >= size) {
- throw logic_error("Невалидный индекс");
- }
- if (index == 0) {
- Pop_front();
- return;
- }
- if (index == size) {
- Pop_back();
- return;
- }
- if (index > 0 && index < size) {
- Node* temp = head;
- for (int i = 0; i < index - 1; i++) {
- temp = temp->next;
- }
- Node* buf = temp->next->next;
- delete temp->next;
- temp->next = buf;
- temp->next->prev = temp;
- size--;
- }
- }
- bool operator==(const List& other) {
- if (this->size != other.size) {
- return false;
- }
- Node* temp = head;
- Node* other_temp = other.head;
- while (temp != nullptr) {
- if (temp->value != other_temp->value) {
- return false;
- }
- temp = temp->next;
- other_temp = other_temp->next;
- }
- return true;
- }
- bool operator!=(const List& other) {
- return !(*this == other);
- }
- T& operator[] (const int index) {
- if (index < 0 || index >= size) {
- throw logic_error("Невалидный индекс");
- }
- Node* temp = head;
- for (int i = 0; i < index; i++) {
- temp = temp->next;
- }
- return temp->value;
- }
- const T& operator[] (const int index) const {
- if (index < 0 || index >= size) {
- throw logic_error("Невалидный индекс");
- }
- Node* temp = head;
- for (int i = 0; i < index; i++) {
- temp = temp->next;
- }
- return temp->value;
- }
- int Find(const T& value) {
- if (size == 0) {
- throw runtime_error("Попытка поиска в пустом контейнере");
- }
- int i = 0;
- Node* temp = head;
- while (temp != nullptr && temp->value != value) {
- temp = temp->next;
- i++;
- }
- if (temp != nullptr) {
- return i;
- }
- return -1;
- }
- ~List() {
- Clear();
- }
- private:
- struct Node { // двусвязный список состоит из узлов
- T value; // узел хранит информативную часть
- Node* next; // указатель на следующий узел в списке
- Node* prev; // указатель на предыдущий узел
- };
- int size = 0;
- Node* head = nullptr;
- Node* last = nullptr;
- };
- template <typename T>
- class Set {
- public:
- struct Node {
- T value{};
- Node* left = nullptr;
- Node* right = nullptr;
- Node* parent = nullptr;
- };
- Set() : size(0), root(nullptr) {}
- Set(const initializer_list<T>& list) : size(0), root(nullptr) {
- for (const auto& element : list) {
- Insert(element);
- }
- }
- Set(const Set& other) : size(other.size), root(Copy(other.root, nullptr)) {}
- Set(Set&& other) : size(other.size), root(other.root) {
- other.size = 0;
- other.root = nullptr;
- }
- Set& operator=(const Set& other) {
- if (this != &other) {
- Clear();
- size = other.size;
- root = Copy(other.root, nullptr);
- }
- return *this;
- }
- Set& operator=(Set&& other) {
- if (this != &other) {
- Clear();
- size = other.size;
- root = other.root;
- other.root = nullptr;
- other.size = 0;
- }
- return *this;
- }
- Node* Copy(Node* node, Node* parent) {
- if (node == nullptr) {
- return node;
- }
- Node* temp = new Node{ node->value, nullptr, nullptr, parent };
- temp->left = Copy(node->left, temp);
- temp->right = Copy(node->right, temp);
- return temp;
- }
- void Insert(const T& val) {
- if (root == nullptr) {
- root = new Node{ val, nullptr, nullptr, nullptr };
- size++;
- return;
- }
- Node* parent = nullptr;
- Node* node = root;
- while (node != nullptr && node->value != val) {
- parent = node;
- node = node->value > val ? node->left : node->right;
- }
- if (node == nullptr) {
- Node* temp = new Node{ val, nullptr, nullptr, parent };
- parent->value > val ? parent->left = temp : parent->right = temp;
- size++;
- }
- }
- void Erase(const T& val) {
- Node* parent = nullptr;
- Node* node = root;
- while (node != nullptr && node->value != val) {
- parent = node;
- node = node->value > val ? node->left : node->right;
- }
- if (node != nullptr) {
- if (node->left == nullptr && node->right == nullptr) {
- if (node == root) {
- root = nullptr;
- }
- else {
- node == parent->right ? parent->right = nullptr : parent->left = nullptr;
- }
- delete node;
- }
- else if (node->left == nullptr) {
- node->right->parent = node->parent;
- if (node == root) {
- root = node->right;
- }
- else {
- node == parent->right ? parent->right = node->right : parent->left = node->right;
- }
- delete node;
- }
- else if (node->right == nullptr) {
- node->left->parent = node->parent;
- if (node == root) {
- root = node->left;
- }
- else {
- node == parent->right ? parent->right = node->left : parent->left = node->left;
- }
- delete node;
- }
- else {
- Node* temp = Min(node->right);
- if (temp->right) {
- temp->right->parent = temp->parent;
- }
- temp == temp->parent->right ? temp->parent->right = temp->right : temp->parent->left = temp->right;
- node->value = temp->value;
- delete temp;
- }
- size--;
- }
- }
- Node* Find(const T& val) {
- Node* node = root;
- while (node != nullptr && node->value != val) {
- node = node->value > val ? node->left : node->right;
- }
- return node;
- }
- int Count(const T& val) {
- return Find(val) == nullptr ? 0 : 1;
- }
- Node* Next(Node* node) {
- if (node) {
- if (node->right) {
- return Min(node->right);
- }
- Node* temp = node->parent;
- while (temp != nullptr && node->value > temp->value) {
- temp = temp->parent;
- }
- return temp;
- }
- }
- Node* Prev(Node* node) {
- if (node) {
- if (node->left) {
- return Max(node->left);
- }
- Node* temp = node->parent;
- while (temp != nullptr && node->value < temp->value) {
- temp = temp->parent;
- }
- return temp;
- }
- }
- Node* Begin() {
- return Min(root);
- }
- Node* End() {
- return Max(root);
- }
- Node* Min(Node* node) {
- if (node != nullptr)
- while (node->left != nullptr)
- node = node->left;
- return node;
- }
- Node* Max(Node* node) {
- if (node != nullptr)
- while (node->right != nullptr)
- node = node->right;
- return node;
- }
- void Print() {
- Print(root);
- cout << endl;
- }
- void Print(Node* node) {
- if (node != nullptr) {
- Print(node->left);
- cout << node->value << " ";
- Print(node->right);
- }
- }
- void Clear() {
- Clear(root);
- root = nullptr;
- size = 0;
- }
- void Clear(Node* node) {
- if (node == nullptr) {
- return;
- }
- Clear(node->left);
- Clear(node->right);
- delete node;
- }
- int Size() {
- return size;
- }
- bool Compare(Node* a, Node* b) {
- if (a == nullptr && b == nullptr) {
- return true;
- }
- return (a != nullptr && b != nullptr) && (a->value == b->value) && Compare(a->left, b->left) && Compare(a->right, b->right);
- }
- bool operator==(const Set& other) {
- return Compare(root, other.root);
- }
- bool operator!=(const Set& other) {
- return !(*this == other);
- }
- ~Set() {
- Clear(root);
- }
- private:
- int size = 0;
- Node* root = nullptr;
- };
- int main() {
- setlocale(LC_ALL, "ru");
- try {
- // проверить работу всех классов
- }
- catch (const logic_error& ex) {
- cout << "1 " << ex.what() << endl;
- }
- catch (const runtime_error& ex) {
- cout << "2 " << ex.what() << endl;
- }
- catch (...) {
- cout << "3 Другая ошибка" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment