当前位置: 首页> 财经> 股票 > 在线美图秀秀在线制作_兰州今天发生的重大新闻_网络营销软件商城_重庆百度快照优化排名

在线美图秀秀在线制作_兰州今天发生的重大新闻_网络营销软件商城_重庆百度快照优化排名

时间:2025/7/10 12:09:27来源:https://blog.csdn.net/qq_43355454/article/details/146456939 浏览次数:1次
在线美图秀秀在线制作_兰州今天发生的重大新闻_网络营销软件商城_重庆百度快照优化排名

C++哈希计数器,代码见下:

#include<iostream>using namespace std;template<typename KeyType, typename ValueType>
class HashNode {
public:KeyType key;ValueType value;HashNode* next;HashNode(const KeyType& key, const ValueType& value) {this->key = key;this->value = value;this->next = NULL;}
};template<typename KeyType, typename ValueType>
class HashTable {
private:int size;HashNode<KeyType, ValueType>** table;int hash(const KeyType& key) const {int hashkey = key % size;if (hashkey < 0) {hashkey += size;}return hashkey;}
public:HashTable(int size = 256);~HashTable();void insert(const KeyType& key, const ValueType& value);void remove(const KeyType& key);bool find(const KeyType& key, ValueType& value) const;
};template<typename KeyType, typename ValueType>
HashTable<KeyType, ValueType>::HashTable(int size) {this->size = size;this->table = new HashNode<KeyType, ValueType>* [size];for (int i = -0; i < size; ++i) {this->table[i] = NULL;}
}template<typename KeyType, typename ValueType>
HashTable<KeyType, ValueType>::~HashTable() {for (int i = 0; i < size; ++i) {if (table[i]) {HashNode<KeyType, ValueType>* current = table[i];while (current) {HashNode<KeyType, ValueType>* next = current->next;delete current;current = next;}table[i] = NULL;}}delete[] table;table = NULL;
}template<typename KeyType, typename ValueType>
void HashTable<KeyType, ValueType>::insert(const KeyType& key, const ValueType& value) {int index = hash(key);HashNode<KeyType, ValueType>* now = new HashNode<KeyType, ValueType>(key, value);if (table[index] == NULL) {table[index] = now;}else {now->next = table[index];table[index] = now;}
}template<typename KeyType, typename ValueType>
void HashTable<KeyType, ValueType>::remove(const KeyType& key) {int index = hash(key);if (table[index]) {if (table[index]->key == key) {HashNode<KeyType, ValueType>* next = table[index]->next;delete table[index];table[index] = next;}else {HashNode<KeyType, ValueType>* current = table[index];while (current->next && current->next->key != key) {current = current->next;}if (current->next) {HashNode<KeyType, ValueType>* next = current->next->next;delete current->next;current->next = next;}}}
}template<typename KeyType, typename ValueType>
bool HashTable<KeyType, ValueType>::find(const KeyType& key, ValueType& value) const {int index = hash(key);if (table[index]) {if (table[index]->key == key) {value = table[index]->value;return true;}else {HashNode<KeyType, ValueType>* current = table[index];while (current->next && current->next->key != key) {current = current->next;}if (current->next) {value = current->next->value;return true;}}}return false;
}template<typename KeyType>
class HashCounter {
private:int* counter;int counterIndex;int counterSize;HashTable<KeyType, int>* hash;
public:HashCounter(int size = 256);~HashCounter();void reset();int add(const KeyType& key);int sub(const KeyType& key);int get(const KeyType& key);
};template<typename KeyType>
HashCounter<KeyType>::HashCounter(int size) {counterSize = size;counterIndex = 0;counter = new int[counterSize];hash = NULL;reset();
}template<typename KeyType>
HashCounter<KeyType>::~HashCounter() {delete[]counter;if (hash) {delete hash;hash = NULL;}
}template<typename KeyType>
void HashCounter<KeyType>::reset() {if (hash) {delete hash;hash = NULL;}hash = new HashTable<KeyType, int>(counterSize);counterIndex = 0;for (int i = 0; i < counterSize; ++i) {counter[i] = 0;}
}template<typename KeyType>
int HashCounter<KeyType>::add(const KeyType& key) {int idx;if (!hash->find(key, idx)) {idx = counterIndex++;hash->insert(key, idx);}return ++counter[idx];
}template<typename KeyType>
int HashCounter<KeyType>::sub(const KeyType& key) {int idx;if (hash->find(key, idx)) {return --counter[idx];}return 0;
}template<typename KeyType>
int HashCounter<KeyType>::get(const KeyType& key) {int idx;if (hash->find(key, idx)) {return counter[idx];}return 0;
}int main() {HashCounter<long long> hc(1000);hc.add(14);hc.add(14);hc.add(14);hc.add(14);hc.add(14);hc.add(14);hc.sub(14);cout << hc.get(14) << endl;}

关键字:在线美图秀秀在线制作_兰州今天发生的重大新闻_网络营销软件商城_重庆百度快照优化排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: