为MyVector增加了必须的迭代器操作

This commit is contained in:
梦凌汐 2024-12-18 22:58:01 +08:00
parent 959c73bfc6
commit b20d8aa6d0
3 changed files with 69 additions and 4 deletions

View File

@ -15,10 +15,10 @@ private:
std::string content_; std::string content_;
RichText coloredContent_; RichText coloredContent_;
std::string ruleName_; std::string ruleName_;
std::vector<std::string> lineedContent_; MyVector<std::string> lineedContent_;
std::vector<std::string> split(std::string content) { MyVector<std::string> split(std::string content) {
std::vector<std::string> lineedContent; MyVector<std::string> lineedContent;
std::string tempStr = content_ + "\n"; std::string tempStr = content_ + "\n";
size_t pos = tempStr.find("\n"); size_t pos = tempStr.find("\n");
lineedContent.clear(); lineedContent.clear();
@ -30,7 +30,7 @@ private:
return lineedContent; return lineedContent;
} }
std::string assemble(std::vector<std::string> lineedContent) { std::string assemble(MyVector<std::string> lineedContent) {
std::string content = ""; std::string content = "";
for(auto line : lineedContent) { for(auto line : lineedContent) {
content += line + "\n"; content += line + "\n";

View File

@ -173,6 +173,57 @@ public:
} }
return m_data[index]; 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 #endif // MY_VECTOR_H

14
utils/ActionManager.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef ACTIONMANAGER_H
#define ACTIONMANAGER_H
enum class EditAction {
Initialize,
Insert,
Delete,
};
class ActionManager {
}
#endif