zoukankan      html  css  js  c++  java
  • 自己动手实现线性映射,哈希映射

    一个简单的线性映射:

    #include<vector>
    #include<iostream>
    using namespace std;
    template<class Key,class Value>
    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<DataEntry> arr;
        int current_size;
    };
    int main()
    {
        LinearMap<string, int> lm;
        lm.Put("tom", 99);
        lm.Put("Jack", 88);
        lm.Put("ly", 77);
        cout << lm.Get("Jack") << endl;
        return 0;
    }
    View Code
    手里拿着一把锤子,看什么都像钉子,编程界的锤子应该就是算法了吧!
  • 相关阅读:
    ioi1998 Polygon
    [Noip模拟题]Seq
    [noip模拟]分组行动
    入门OJ:photo
    Sgu167 I-country
    入门OJ:简单的网络游戏
    入门OJ:Coin
    ATT&CK实战系列
    Metasploit Framework(二)
    RoarCTF 2019
  • 原文地址:https://www.cnblogs.com/chess/p/4853160.html
Copyright © 2011-2022 走看看