博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己动手实现线性映射,哈希映射
阅读量:6994 次
发布时间:2019-06-27

本文共 986 字,大约阅读时间需要 3 分钟。

一个简单的线性映射:

#include
#include
using namespace std;template
class LinearMap{public: LinearMap(int size = 101) :arr(size) { current_size = 0; } void Put(const Key&k, const Value&v) { arr[current_size] = DataEntry(k, v); ++current_size; } Value Get(const Key&k) { for (int i = 0; i < current_size; ++i) { if (arr[i].key == k) return arr[i].value; } return Value(); }private: struct DataEntry{ Key key; Value value; DataEntry(const Key&k = Key(), const Value&v = Value()) :key(k), value(v){}; }; vector
arr; int current_size;};int main(){ LinearMap
lm; lm.Put("tom", 99); lm.Put("Jack", 88); lm.Put("ly", 77); cout << lm.Get("Jack") << endl; return 0;}
View Code

 

转载于:https://www.cnblogs.com/chess/p/4853160.html

你可能感兴趣的文章