From bb2ca6c15cd3814e34d25909779bbafee2c8e522 Mon Sep 17 00:00:00 2001 From: Hong Ji Whan <113449410+HJW-storage@users.noreply.github.com> Date: Fri, 15 Dec 2023 12:35:54 +0900 Subject: [PATCH 1/3] Add files via upload --- submission/ex09.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 submission/ex09.cpp diff --git a/submission/ex09.cpp b/submission/ex09.cpp new file mode 100644 index 0000000..7d82c26 --- /dev/null +++ b/submission/ex09.cpp @@ -0,0 +1,84 @@ +#include +#include +#include + +using namespace std; + +template +class MyVector { +private: + T* data; + std::size_t size; + std::size_t capacity; + +public: + // »ý¼ºÀÚ + MyVector() : data(nullptr), size(0), capacity(3) { + data = new T[capacity]; // µ¿Àû¸Þ¸ð¸® ÇÒ´ç + } + + // ¼Ò¸êÀÚ + ~MyVector() { + delete[] data; // µ¿Àû¸Þ¸ð¸® ÇØÁ¦ + } + + T& operator[](std::size_t i) { + if (i < size) { + return data[i]; + } + throw std::out_of_range("Invalid index"); + } + + void push_back(const T& value) { + if (size >= capacity) { + // ¸Þ¸ð¸® »õ·Î ÇÒ´ç + capacity *= 2; + T* newData = new T[capacity]; + // ±âÁ¸¿¡ ÀÖ´ø ¸Þ¸ð¸®ÀÇ °ªÀ» »õ·Î ÇÒ´çµÈ °÷¿¡ º¹»ç + /*for (std::size_t i = 0; i < size; i++) { + newData[i] = data[i]; + }*/ + copy(data, data + size, newData); // º¹»ç ÇÔ¼ö¸¦ »ç¿ë + + // º¹»ç¸¦ ´ÙÇßÀ¸¹Ç·Î ±âÁ¸ ¸Þ¸ð¸®´Â ÇØÁ¦ÇÑ´Ù. + delete[] data; + + // ±âÁ¸ Æ÷ÀÎÅÍ ´ëü + data = newData; + } + data[size] = value; + size++; + + size_t c_size = get_size(); + cout << "v.get_size() == " << c_size << "\n"; + } + + void pop_back() { + if (size > 0) { + size--; + } + } + + size_t get_size() const { + return size; + } + + T& at(size_t index) { + if (index >= 0 && index < size) { + return data[index]; + } + throw out_of_range("Invalid index"); + } +}; + +int main() { + MyVector v; + + v.push_back(1); // v.get_size() == 1 + v.push_back(2); // v.get_size() == 2 + v.push_back(3); // v.get_size() == 3 + v.push_back(4); // v.get_size() == 4 + v.push_back(5); // v.get_size() == 5 + + return 0; +} \ No newline at end of file From e8c020c8f195f7f6bb561b366ff51f42e1964813 Mon Sep 17 00:00:00 2001 From: Hong Ji Whan <113449410+HJW-storage@users.noreply.github.com> Date: Fri, 15 Dec 2023 12:36:22 +0900 Subject: [PATCH 2/3] Create exericise9 --- submission/exericise9 | 1 + 1 file changed, 1 insertion(+) create mode 100644 submission/exericise9 diff --git a/submission/exericise9 b/submission/exericise9 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/submission/exericise9 @@ -0,0 +1 @@ + From adfb3724c11d170811c42c9032cfaf1610070ec2 Mon Sep 17 00:00:00 2001 From: Hong Ji Whan <113449410+HJW-storage@users.noreply.github.com> Date: Fri, 15 Dec 2023 15:28:33 +0900 Subject: [PATCH 3/3] Add files via upload --- submission/ex10.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 submission/ex10.cpp diff --git a/submission/ex10.cpp b/submission/ex10.cpp new file mode 100644 index 0000000..0f69fb6 --- /dev/null +++ b/submission/ex10.cpp @@ -0,0 +1,105 @@ +#include +#include +#include + +using namespace std; + +template +class MyVector { +private: + T* element; + size_t capacity; + size_t size; + +public: + // ±âº» »ý¼ºÀÚ + MyVector() : element(nullptr), capacity(3), size(0) {} + + // ¼Ò¸êÀÚ + ~MyVector() { + delete[] element; + } + + // º¹»ç»ý¼ºÀÚ + MyVector(const MyVector& other) : element(new T[other.capacity]), capacity(other.capacity), size(other.size) { + std::copy(other.element, other.element + other.size, element); + } + + // º¹»ç´ëÀÔ»ý¼ºÀÚ + MyVector& operator=(const MyVector& other) { + if (this != &other) { + delete[] element; + element = new T[other.capacity]; + capacity = other.capacity; + size = other.size; + std::copy(other.element, other.element + other.size, element); + } + return *this; + } + + + MyVector(MyVector&& other) noexcept : element(other.element), capacity(other.capacity), size(other.size) { + other.element = nullptr; + other.capacity = 0; + other.size = 0; + } + + MyVector& operator=(MyVector&& other) noexcept { + if (this != &other) { + delete[] element; + element = other.element; + capacity = other.capacity; + size = other.size; + + other.element = nullptr; + other.capacity = 0; + other.size = 0; + } + return *this; + } + + void push_back(const T& value) { + if (element == nullptr || size >= capacity) { + // ¸Þ¸ð¸® »õ·Î ÇÒ´ç + capacity *= 2; + T* newData = new T[capacity]; + // ±âÁ¸¿¡ ÀÖ´ø ¸Þ¸ð¸®ÀÇ °ªÀ» »õ·Î ÇÒ´çµÈ °÷¿¡ º¹»ç + /*for (std::size_t i = 0; i < size; i++) { + newData[i] = data[i]; + }*/ + copy(element, element + size, newData); // º¹»ç ÇÔ¼ö¸¦ »ç¿ë + + // º¹»ç¸¦ ´ÙÇßÀ¸¹Ç·Î ±âÁ¸ ¸Þ¸ð¸®´Â ÇØÁ¦ÇÑ´Ù. + delete[] element; + + // ±âÁ¸ Æ÷ÀÎÅÍ ´ëü + element = newData; + } + element[size] = value; + size++; + + //cout << "v.get_size() == " << size << "\n"; + } + + size_t size() const { + return size; + } + +}; + +int main() { + MyVector v; + + v.push_back(1); + v.push_back(2); + v.push_back(3); + v.push_back(4); + v.push_back(5); + + MyVector v2 = std::move(v); + + std::cout << "v.get_size() = " << v.size() << "\n"; + std::cout << "v2.get_size() = " << v2.size() << "\n"; + + return 0; +} \ No newline at end of file