C++手撕 --基本数据结构的简单实现(2)

C++面试手撕代码----基本数据结构的简单实现(2)

1.哈希表(unordered_map)

#include <iostream>
#include <utility> // for pair
#include <vector>
#include <list> // for list
#include <functional> // for std::hash

using namespace std;
template<typename K, typename V>

class myHashMap {
private:
	const int TABLE_SIZE = 10;
	vector<list<pair<K, V>>> table;

	int myhash(const K& key) const {
		return hash<K>()(key) % TABLE_SIZE;
	}

public:
	myHashMap() : table(TABLE_SIZE) {}
	~myHashMap() {}

	void put(const K& key, const V& val) {
		int index = myhash(key);
		for (auto& pair : table[index]) {
			if (pair.first == key) {
				pair.second = val;
				return;
			}
		}

		table[index].emplace_back(make_pair(key, val));
	}

	V& operator[](const K& key) {
		int index = myhash(key);
		for (auto& pair : table[index]) {
			if (pair.first == key) {
				return pair.second;
			}
		}

		table[index].emplace_back(make_pair(key, V()));
		return table[index].back().second;
	}

	bool get(const K& key, V& val) const {
		int index = myhash(key);
		for (const auto& pair : table[index]) {
			if (pair.first == key) {
				val = pair.second;
				return true;;
			}
		}

		cout << "Key : " << key << "can not find value." << endl;
		return false;
	}

	bool remove(const K& key) {
		int index = myhash(key);
		auto index_table = table[index];

		for (auto it = index_table.begin(); it != index_table.end(); ++it) {
			if (it->first == key) {
				index_table.erase(it);
				return true;
			}
		}

		return false;
	}

	void show() const {
		cout << "{ Key : Value }:" << endl;
		for (int i = 0; i < TABLE_SIZE; ++i) {
			for (const auto& pair : table[i]) {
				cout << "{ " << pair.first << " : " << pair.second << " }" << endl;
			}
		}
		cout << endl;
	}
};

2.哈希集合(unordered_set)

#include <vector>
#include <list>
#include <iostream>
#include <functional>  // for std::hash

using namespace std;

template<typename T>
class hashSet {
private:
    const int TABLE_SIZE = 10;
    vector<list<T>> table;

    // 哈希函数
    int myHash(const T& val) {
        return std::hash<T>()(val) % TABLE_SIZE;
    }

public:
    hashSet() : table(TABLE_SIZE) {}
    ~hashSet() {}

    // 插入元素
    void put(const T& val) {
        int index = myHash(val);  // 使用哈希函数计算桶的索引
        for (auto& v : table[index]) {
            if (v == val) {
                return;  // 如果元素已存在,则不插入
            }
        }
        table[index].emplace_back(val);  // 插入元素
    }

    // 检查元素是否存在
    bool check(const T& val) {
        int index = myHash(val);
        for (auto& v : table[index]) {
            if (v == val) {
                return true;  // 找到元素,返回true
            }
        }
        return false;  // 元素不存在,返回false
    }

    // 删除元素
    bool remove(const T& val) {
        int index = myHash(val);
        auto& list = table[index];
        for (auto it = list.begin(); it != list.end(); ++it) {
            if (*it == val) {
                list.erase(it);  // 删除找到的元素
                return true;
            }
        }
        return false;  // 删除失败,返回false
    }

    // 输出集合中的所有元素
    void show() const {
        cout << "Set: " << endl;
        for (int i = 0; i < TABLE_SIZE; ++i) {
            if (!table[i].empty()) {
                for (auto& v : table[i]) {
                    cout << v << " ";
                }
            }
        }
	cout << endl;
    }
};
posted @ 2024-11-06 20:15  风枕子  阅读(75)  评论(0)    收藏  举报