最近在看java的LInkedHashMap,提到了LRU算法,就查了下相关的资料,电脑是linux系统,就想着在该环境下试着实现吧,相对来说比winow开发麻烦一点,主要涉及到头文件的引用,命名空间的引用
关键点在于,应用HashMap和双向链表
(1)HashMap在于创建Key值和节点地址的对应关系
(2)双向链表在于控制节点的顺序,消除链表尾部的数据
代码如下,以备复习使用
#include<iostream>
#include<vector>
#include<hash_map>
#include<hash_set>
#include "HASH_MAP_HPP.h"
using namespace std;
using namespace __gnu_cxx;
/*namespace __gun_cxx
{
template<> struct hash<string>
{
size_t operator()(const string& s) const
{
return __stl_hash_string(s.c_str());
}
};
}*/
struct LRUCacheEntry{
string key;
string value;
struct LRUCacheEntry * prev;
struct LRUCacheEntry * next;
};
class LRUCache{
public:
LRUCache(size_t size){
entries = new LRUCacheEntry[size];
for(int i=0;i<size;i++){
_freeEntries.push_back(entri