lz78中字典实现

  1. #include <map>
  2. #include <string>
  3. class Dictionary  
  4. {
  5. public:
  6.     std::string get_perfix(long mask);
  7.     long get_mask(const std::string perfix);
  8.     bool is_exist(const std::string member);
  9.     void add(const std::string word);
  10.     Dictionary();
  11.     virtual ~Dictionary();
  12. private:
  13.     long index;
  14.     std::map<std::string, long> store; 
  15. };
  16. Dictionary::Dictionary()
  17. {
  18.     index = 0;
  19. }
  20. Dictionary::~Dictionary()
  21. {
  22. }
  23. void Dictionary::add(const std::string word)
  24. {
  25.     this->store[word] = ++index;
  26. }
  27. bool Dictionary::is_exist(const std::string member)
  28. {
  29.     std::map<std::string, long>::iterator pos;
  30.     pos = this->store.find(member);
  31.     if (pos != store.end())
  32.     {
  33.         return true;
  34.     }
  35.     else
  36.     {
  37.         return false;
  38.     }
  39. }
  40. long Dictionary::get_mask(const std::string perfix)
  41. {
  42.     if ((index==0) || (perfix==""))
  43.     {
  44.         return 0;
  45.     }
  46.     else
  47.     {
  48.             std::map<std::string, long>::iterator pos;
  49.             pos = this->store.find(perfix);
  50.             if (pos != store.end())
  51.             {
  52.                 return pos->second;
  53.             }
  54.             else
  55.             {
  56.                 return 0;
  57.             }
  58.     }
  59. }
  60. std::string Dictionary::get_perfix(long mask)
  61. {
  62.     if (mask != 0)
  63.     {
  64.         std::map<std::string, long>::iterator pos;
  65.         for (pos = this->store.begin(); pos != store.end(); pos++)
  66.         {
  67.             if (pos->second == mask)
  68.             {
  69.                 return pos->first;
  70.             }
  71.         }
  72.     }
  73.     return "";
  74. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值