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;
- int main() {
- setlocale(LC_ALL, "ru");
- try {
- Vector<int> my_vector;
- my_vector.Push_back(10);
- cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
- my_vector.Erase(0);
- cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
- for (int i = 0; i < 10; i++) {
- my_vector.Push_back(i + 1);
- }
- for (int i = 0; i < my_vector.Size(); i++) {
- cout << my_vector[i] << " ";
- }
- cout << endl;
- cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
- my_vector[2] = 13;
- my_vector.Insert(4, 12);
- for (int i = 0; i < my_vector.Size(); i++) {
- cout << my_vector[i] << " ";
- }
- cout << endl;
- cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
- Vector<int> my_vector2;
- //my_vector2.Pop_back();
- for (int i = 0; i < 15; i++) {
- my_vector2.Push_back(i + 1);
- }
- Vector<int> my_vector3(my_vector2);
- Vector<int> my_vector4 = my_vector;
- cout << boolalpha;
- cout << (my_vector == my_vector3) << endl;
- cout << (my_vector != my_vector3) << endl;
- cout << my_vector3[3] << endl;
- Vector<int> my_vector5(move(my_vector3));
- cout << "Size: " << my_vector5.Size() << " Capacity: " << my_vector5.Capacity() << endl;
- cout << "Size: " << my_vector3.Size() << " Capacity: " << my_vector3.Capacity() << endl;
- my_vector5 = move(my_vector4);
- cout << "Size: " << my_vector5.Size() << " Capacity: " << my_vector5.Capacity() << endl;
- cout << "Size: " << my_vector3.Size() << " Capacity: " << my_vector3.Capacity() << endl;
- cout << Vector<int>::GetTotalNumberElements() << endl;
- Vector<string> my_vector6;
- for (int i = 0; i < 8; i++) {
- my_vector6.Push_back("123");
- }
- Vector<string> my_vector7(my_vector6);
- Vector<int> my_vector8{ 1, 2, 3, 4 };
- cout << my_vector6[0] << " Size: " << my_vector6.Size() << " Capacity: " << my_vector6.Capacity() << endl;
- cout << my_vector7[0] << " Size: " << my_vector7.Size() << " Capacity: " << my_vector7.Capacity() << endl;
- cout << my_vector8[0] << " Size: " << my_vector8.Size() << " Capacity: " << my_vector8.Capacity() << endl;
- cout << Vector<int>::GetTotalNumberElements() << endl;
- cout << Vector<string>::GetTotalNumberElements() << endl;
- }
- catch (const logic_error& er) {
- cout << "_1_ " << er.what() << endl;
- }
- catch (const runtime_error& er) {
- cout << "_2_ " << er.what() << endl;
- }
- catch (const bad_alloc& er) {
- cout << "_3_ " << er.what() << endl;
- }
- catch (...) {
- cout << "_4_ " << "Другая ошибка" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment