• 4607阅读
  • 0回复

非排序的key-value容器QKeyValueList [复制链接]

上一主题 下一主题
离线chuong
 

只看楼主 倒序阅读 楼主  发表于: 2012-12-13
因为程序需要存储Key-Value数据,而QMap又会对key值自动排序,所以参考一些论坛回帖,写了这个类。由于没有用hash排序,我认为该类不适合大数据存储、查询。附上代码给需要的朋友。

  1. template <typename KeyType, typename ValueType>
  2. class QKeyValueList
  3. {
  4. public:
  5.     void insert(KeyType key, ValueType value)
  6.     {
  7.         if(KeyList.contains(key)){
  8.             ValueList[KeyList.indexOf(key)] = value;
  9.         }else{
  10.             KeyList.append(key);
  11.             ValueList.append(value);
  12.         }
  13.     }
  14.     void remove(KeyType key)
  15.     {
  16.         if(KeyList.contains(key)){
  17.             int i = KeyList.indexOf(key);
  18.             ValueList.removeAt(i);
  19.             KeyList.removeAt(i);
  20.         }
  21.     }
  22.     ValueType value(KeyType key)
  23.     {
  24.         if(KeyList.contains(key)){
  25.             return ValueList.value(KeyList.indexOf(key));
  26.         }else{
  27.             return NULL;
  28.         }
  29.     }
  30.     int count()
  31.     {
  32.         return KeyList.size();
  33.     }
  34. private:
  35.     QList<KeyType> KeyList;
  36.     QList<ValueType> ValueList;
  37. };

4条评分好评度+1贡献值+1金钱+10威望+1
zwk0704 好评度 +1 - 2017-04-18
zwk0704 贡献值 +1 - 2017-04-18
zwk0704 威望 +1 - 2017-04-18
zwk0704 金钱 +10 - 2017-04-18
快速回复
限100 字节
 
上一个 下一个