Created
June 28, 2025 07:36
-
-
Save Denkotleta221/ba2e38910cb442f1ca01ace0c556ca92 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <iostream> | |
| #include <windows.h> | |
| #include <algorithm> | |
| #include <ctime> | |
| using namespace std; | |
| class Vector { | |
| unsigned int capacity = 10; | |
| int* data = new int[capacity]; | |
| unsigned int length = 0; | |
| public: | |
| Vector() {} | |
| ~Vector() { | |
| cout << "D-TOR\n"; | |
| delete[] data; | |
| } | |
| Vector(const Vector& original) { | |
| cout << "COPY C-TOR\n"; | |
| length = original.length; | |
| capacity = original.capacity; | |
| data = new int[capacity]; | |
| for (int i = 0; i < length; i++) | |
| data[i] = original.data[i]; | |
| } | |
| void AddToBack(int value) { | |
| if (length >= capacity) | |
| resize(capacity * 2); | |
| data[length++] = value; | |
| } | |
| void AddToFront(int value) { | |
| if (length >= capacity) | |
| resize(capacity * 2); | |
| for (int i = length; i > 0; --i) | |
| data[i] = data[i - 1]; | |
| data[0] = value; | |
| length++; | |
| } | |
| void RemoveFromBack() { | |
| if (length == 0) | |
| cout << "Масив порожній!\n"; | |
| else | |
| length--; | |
| } | |
| void RemoveFromFront() { | |
| if (length == 0) { | |
| cout << "Масив порожній!\n"; | |
| return; | |
| } | |
| for (int i = 0; i < length - 1; i++) | |
| data[i] = data[i + 1]; | |
| length--; | |
| } | |
| void Insert(int value, int index) { | |
| if (index < 0 || index >(int)length) { | |
| cout << "Некоректний індекс!\n"; | |
| return; | |
| } | |
| if (length >= capacity) | |
| resize(capacity * 2); | |
| for (int i = length; i > index; --i) | |
| data[i] = data[i - 1]; | |
| data[index] = value; | |
| length++; | |
| } | |
| void RemoveByIndex(int index) { | |
| if (index < 0 || index >= (int)length) { | |
| cout << "Некоректний індекс!\n"; | |
| return; | |
| } | |
| for (int i = index; i < length - 1; ++i) | |
| data[i] = data[i + 1]; | |
| length--; | |
| } | |
| void RemoveByValue(int value) { | |
| int i = 0; | |
| while (i < (int)length) { | |
| if (data[i] == value) | |
| RemoveByIndex(i); | |
| else | |
| i++; | |
| } | |
| } | |
| void Sort() { | |
| sort(data, data + length); | |
| } | |
| void Reverse() { | |
| for (int i = 0; i < length / 2; i++) | |
| swap(data[i], data[length - 1 - i]); | |
| } | |
| void Shuffle() { | |
| for (int i = 0; i < length; i++) { | |
| int j = rand() % length; | |
| swap(data[i], data[j]); | |
| } | |
| } | |
| void Print() const { | |
| if (length == 0) { | |
| cout << "Масив порожній!\n"; | |
| return; | |
| } | |
| cout << "Ємність: " << capacity << ", Кількість: " << length << "\n"; | |
| for (int i = 0; i < length; i++) | |
| cout << data[i] << " "; | |
| cout << "\n"; | |
| } | |
| friend ostream& operator<<(ostream& out, const Vector& v) { | |
| for (int i = 0; i < v.length; ++i) | |
| out << v.data[i] << " "; | |
| return out; | |
| } | |
| friend istream& operator>>(istream& in, Vector& v) { | |
| int count, val; | |
| cout << "Скільки елементів ввести? "; | |
| in >> count; | |
| for (int i = 0; i < count; ++i) { | |
| cout << "Введіть елемент #" << (i + 1) << ": "; | |
| in >> val; | |
| v.AddToBack(val); | |
| } | |
| return in; | |
| } | |
| int& operator[](int index) { | |
| if (index < 0 || index >= (int)length) { | |
| cout << "Індекс виходить за межі!\n"; | |
| exit(1); | |
| } | |
| return data[index]; | |
| } | |
| bool operator==(const Vector& other) const { | |
| if (length != other.length) | |
| return false; | |
| for (int i = 0; i < length; ++i) | |
| if (data[i] != other.data[i]) | |
| return false; | |
| return true; | |
| } | |
| bool operator!=(const Vector& other) const { | |
| return !(*this == other); | |
| } | |
| private: | |
| void resize(unsigned int newCap) { | |
| int* temp = new int[newCap]; | |
| for (int i = 0; i < length; i++) | |
| temp[i] = data[i]; | |
| delete[] data; | |
| data = temp; | |
| capacity = newCap; | |
| } | |
| }; | |
| int main() { | |
| SetConsoleOutputCP(1251); | |
| srand(time(0)); | |
| Vector v; | |
| v.AddToBack(10); v.AddToBack(20); v.AddToBack(30); | |
| v.Print(); | |
| v.AddToFront(5); | |
| v.Print(); | |
| v.RemoveFromBack(); | |
| v.Print(); | |
| v.RemoveFromFront(); | |
| v.Print(); | |
| v.Insert(15, 1); | |
| v.Print(); | |
| v.RemoveByIndex(1); | |
| v.Print(); | |
| v.AddToBack(30); v.AddToBack(30); v.AddToBack(30); | |
| v.Print(); | |
| v.RemoveByValue(30); | |
| v.Print(); | |
| v.AddToBack(3); v.AddToBack(1); v.AddToBack(2); | |
| v.Sort(); | |
| v.Print(); | |
| v.Reverse(); | |
| v.Print(); | |
| v.Shuffle(); | |
| v.Print(); | |
| cout << "v[0] = " << v[0] << "\n"; | |
| Vector v2 = v; | |
| cout << (v == v2 ? "Однакові\n" : "Різні\n"); | |
| v2.AddToBack(999); | |
| cout << (v != v2 ? "Різні\n" : "Однакові\n"); | |
| Vector userVec; | |
| cin >> userVec; | |
| cout << "Введено: " << userVec << "\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment