⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplemap.h

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 H
字号:
#ifndef _SIMPLEMAP_H
#define _SIMPLEMAP_H

#include "tvector.h"

// simple map, supports (for map m):
//    m.insert(Key,Value);
//    v = m.getValue(k);    // returns default Value() if k not found


template <class Key,class Value> class SimpleMapIterator;

template <class Key, class Value>
class SimpleMap
{
  public:
    SimpleMap()
    { }
    void insert(const Key& k, const Value& v)
    {
        myKeys.push_back(k);
        myValues.push_back(v);
    }
    Value getValue(const Key& key) const
    {   for(int k=0; k < myKeys.size(); k++)
        {   if (myKeys[k] == key) return myValues[k];
        }
        return Value();
    }
    
    
    friend class SimpleMapIterator<Key,Value>;
    
  private:
    tvector<Key>   myKeys;
    tvector<Value> myValues;
};


template <class Key,class Value>
class SimpleMapIterator
{
  public:
    SimpleMapIterator(const SimpleMap<Key,Value>& map)
      : myMap(map),
        myIndex(-1)
    { }
    void Init()
    {   myIndex = 0;
    }
    bool HasMore()
    {   return myIndex < myMap.myKeys.size();
    }
    void Next()
    {   myIndex++;
    }
    Key Current()
    {   return myMap.myKeys[myIndex];
    }
  private:
    const SimpleMap<Key,Value>& myMap;
    int              myIndex;
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -