不使用c++的语言特性,重写一个类型为template<typename K, typename V>的map模板类和对应的map迭代器,并重写运算符
时间: 2024-03-26 08:34:48 浏览: 69
c++运算符重载和模板
好的,我可以为您重写一个类型为`template<typename K, typename V> class MyMap`的map模板类和对应的map迭代器,并重载运算符。以下是代码实现:
```c++
#include <iostream>
#include <utility>
template<typename K, typename V>
class MyMap {
private:
struct Node {
std::pair<K, V> data;
Node* left = nullptr;
Node* right = nullptr;
Node* parent = nullptr;
};
Node* root = nullptr;
size_t size = 0;
public:
class iterator {
private:
Node* node = nullptr;
public:
iterator(Node* n) : node(n) {}
iterator& operator++() {
if (!node) return *this;
if (node->right) {
node = node->right;
while (node->left) {
node = node->left;
}
}
else {
Node* p = node->parent;
while (p && node == p->right) {
node = p;
p = p->parent;
}
node = p;
}
return *this;
}
bool operator==(const iterator& other) const {
return node == other.node;
}
bool operator!=(const iterator& other) const {
return !(*this == other);
}
std::pair<const K&, V&> operator*() const {
return std::make_pair(node->data.first, node->data.second);
}
};
MyMap() {}
~MyMap() {
clear(root);
}
void clear() {
clear(root);
root = nullptr;
size = 0;
}
V& operator[](const K& key) {
Node* n = find_node(key);
if (n) {
return n->data.second;
}
else {
insert_node(key, V{});
return find_node(key)->data.second;
}
}
bool contains(const K& key) const {
return find_node(key) != nullptr;
}
size_t get_size() const {
return size;
}
iterator begin() const {
Node* n = root;
while (n && n->left) {
n = n->left;
}
return iterator(n);
}
iterator end() const {
return iterator(nullptr);
}
private:
void clear(Node* n) {
if (n) {
clear(n->left);
clear(n->right);
delete n;
}
}
Node* find_node(const K& key) const {
Node* n = root;
while (n) {
if (n->data.first == key) {
return n;
}
else if (n->data.first < key) {
n = n->right;
}
else {
n = n->left;
}
}
return nullptr;
}
void insert_node(const K& key, const V& value) {
Node* n = new Node{ std::make_pair(key, value) };
if (!root) {
root = n;
}
else {
Node* p = root;
while (true) {
if (n->data.first < p->data.first) {
if (!p->left) {
p->left = n;
n->parent = p;
break;
}
p = p->left;
}
else {
if (!p->right) {
p->right = n;
n->parent = p;
break;
}
p = p->right;
}
}
}
size++;
}
};
template<typename K, typename V>
bool operator==(const MyMap<K, V>& lhs, const MyMap<K, V>& rhs) {
if (lhs.get_size() != rhs.get_size()) {
return false;
}
for (auto iter_l = lhs.begin(), iter_r = rhs.begin(); iter_l != lhs.end(); ++iter_l, ++iter_r) {
if (*iter_l != *iter_r) {
return false;
}
}
return true;
}
template<typename K, typename V>
bool operator!=(const MyMap<K, V>& lhs, const MyMap<K, V>& rhs) {
return !(lhs == rhs);
}
int main() {
MyMap<int, std::string> map{};
map[1] = "one";
map[2] = "two";
map[3] = "three";
map[4] = "four";
map[5] = "five";
MyMap<int, std::string> map2{};
map2[1] = "one";
map2[2] = "two";
map2[3] = "three";
map2[4] = "four";
map2[5] = "five";
MyMap<int, std::string> map3{};
map3[1] = "one";
map3[2] = "two";
map3[3] = "three";
map3[4] = "four";
map3[5] = "FIVE";
std::cout << std::boolalpha << (map == map2) << std::endl; // true
std::cout << std::boolalpha << (map != map3) << std::endl; // true
std::cout << std::boolalpha << (map2 != map3) << std::endl; // true
return 0;
}
```
这个`MyMap`类是使用二叉搜索树实现的,支持以下操作:
- `clear()`:清空map
- `operator[]`:根据key获取或插入value
- `contains()`:判断map中是否包含key
- `get_size()`:获取map大小
- `begin()`和`end()`:获取map的迭代器
此外,我还重载了`operator==`和`operator!=`,支持两个map实例的比较。
阅读全文