From b20d8aa6d066215aa3fa8ee59c791c545ba8008e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=A6=E5=87=8C=E6=B1=90?= Date: Wed, 18 Dec 2024 22:58:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAMyVector=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E5=BF=85=E9=A1=BB=E7=9A=84=E8=BF=AD=E4=BB=A3=E5=99=A8=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Editor.h | 8 +++---- mystl/my_vector.h | 51 +++++++++++++++++++++++++++++++++++++++++++ utils/ActionManager.h | 14 ++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 utils/ActionManager.h diff --git a/components/Editor.h b/components/Editor.h index ef15693..d071130 100644 --- a/components/Editor.h +++ b/components/Editor.h @@ -15,10 +15,10 @@ private: std::string content_; RichText coloredContent_; std::string ruleName_; - std::vector lineedContent_; + MyVector lineedContent_; - std::vector split(std::string content) { - std::vector lineedContent; + MyVector split(std::string content) { + MyVector lineedContent; std::string tempStr = content_ + "\n"; size_t pos = tempStr.find("\n"); lineedContent.clear(); @@ -30,7 +30,7 @@ private: return lineedContent; } - std::string assemble(std::vector lineedContent) { + std::string assemble(MyVector lineedContent) { std::string content = ""; for(auto line : lineedContent) { content += line + "\n"; diff --git a/mystl/my_vector.h b/mystl/my_vector.h index 0acdd13..862a3f4 100644 --- a/mystl/my_vector.h +++ b/mystl/my_vector.h @@ -173,6 +173,57 @@ public: } return m_data[index]; } + + //实现迭代器 + struct iterator { + T* ptr; + + iterator(T* ptr) : ptr(ptr) {} + T& operator*() const { return *ptr; } + T* operator->() const { return ptr; } + iterator& operator++() { ++ptr; return *this; } + iterator operator++(int) { iterator tmp = *this; ++ptr; return tmp; } + bool operator==(const iterator& other) const { return ptr == other.ptr; } + bool operator!=(const iterator& other) const { return ptr != other.ptr; } + + iterator& operator+(size_t n) { + ptr += n; + return *this; + } + iterator operator+(size_t n) const { + iterator tmp = *this; + tmp.ptr += n; + return tmp; + } + long long operator+(iterator other) const { + return ptr + other.ptr; + } + + iterator& operator-(size_t n) { + ptr -= n; + return *this; + } + iterator operator-(size_t n) const { + iterator tmp = *this; + tmp.ptr -= n; + return tmp; + } + long long operator-(iterator other) const { + return ptr - other.ptr; + } + }; + + iterator begin() { + return iterator(m_data); + } + + iterator end() { + return iterator(m_data + m_size); + } + + void erase(iterator target) { + erase(target - begin()); + } }; #endif // MY_VECTOR_H diff --git a/utils/ActionManager.h b/utils/ActionManager.h new file mode 100644 index 0000000..0ebc330 --- /dev/null +++ b/utils/ActionManager.h @@ -0,0 +1,14 @@ +#ifndef ACTIONMANAGER_H +#define ACTIONMANAGER_H + +enum class EditAction { + Initialize, + Insert, + Delete, +}; + +class ActionManager { + +} + +#endif \ No newline at end of file