在c++中实现LRU缓存-编译错误
Implement a LRU Cache in C++ - compilation error
我需要在c++中实现LRU缓存。我有这个代码,我有问题,而编译:
#include <iostream>
#include <vector>
#include <hash_map>
using namespace std;
using namespace stdext;
template<class K, class T>
struct LRUCacheEntry
{
K key;
T data;
LRUCacheEntry* prev;
LRUCacheEntry* next;
};
template<class K, class T>
class LRUCache
{
private:
hash_map< K, LRUCacheEntry<K,T>* > _mapping;
vector< LRUCacheEntry<K,T>* > _freeEntries;
LRUCacheEntry<K,T> * head;
LRUCacheEntry<K,T> * tail;
LRUCacheEntry<K,T> * entries;
public:
LRUCache(size_t size){
entries = new LRUCacheEntry<K,T>[size];
for (int i=0; i<size; i++)
_freeEntries.push_back(entries+i);
head = new LRUCacheEntry<K,T>;
tail = new LRUCacheEntry<K,T>;
head->prev = NULL;
head->next = tail;
tail->next = NULL;
tail->prev = head;
}
~LRUCache()
{
delete head;
delete tail;
delete [] entries;
}
void put(K key, T data)
{
LRUCacheEntry<K,T>* node = _mapping[key];
if(node)
{
// refresh the link list
detach(node);
node->data = data;
attach(node);
}
else{
if ( _freeEntries.empty() )
{
node = tail->prev;
detach(node);
_mapping.erase(node->key);
node->data = data;
node->key = key;
attach(node);
}
else{
node = _freeEntries.back();
_freeEntries.pop_back();
node->key = key;
node->data = data;
_mapping[key] = node;
attach(node);
}
}
}
T get(K key)
{
LRUCacheEntry<K,T>* node = _mapping[key];
if(node)
{
detach(node);
attach(node);
return node->data;
}
else return NULL;
}
private:
void detach(LRUCacheEntry<K,T>* node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
void attach(LRUCacheEntry<K,T>* node)
{
node->next = head->next;
node->prev = head;
head->next = node;
node->next->prev = node;
}
};
编译resualt:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/backward/hash_map:60,
from Source.C:3:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/backward/backward_warning.h:28:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated.
Source.C:6: error: âstdextâ is not a namespace-name
Source.C:6: error: expected namespace-name before â;â token
Source.C:21: error: ISO C++ forbids declaration of âhash_mapâ with no type
Source.C:21: error: expected â;â before â<â token
Source.C: In member function âvoid LRUCache<K, T>::put(K, T)â:
Source.C:46: error: â_mappingâ was not declared in this scope
Source.C: In member function âT LRUCache<K, T>::get(K)â:
Source.C:77: error: â_mappingâ was not declared in this scope
有人能告诉我如何解决"stdext"问题吗?我想它会解决剩下的错误。TNX !
stdext
是一个MSVC扩展,但您正在使用GCC在linux上编译,因此它不可用。GCC有一个类似的扩展,但是它的头是ext/hash_map
,并且在__gnu_cxx
命名空间中,而不是stdext
。MSVC和GCC的hash_maps都有类似的接口,但如果我没记错的话,它们之间存在细微的差别。
如果可以,使用c++ 11的unordered_map。如果做不到这一点,也许你有TR1和std::tr1::unordered_map
可用。最后,考虑Boost的无序容器(作为TR1和c++ 11接口的基础)。
编辑我刚刚注意到你正在使用GCC 4.4。x, TR1是可用的。唯一的问题是,如果你用MSVC在windows上编译相同的代码,我也不相信TR1在那里可用(我有VC8和VC9可用,它在任何一个都不可用)。我相信他们直接开始在VC10中支持c++ 11库,完全跳过TR1)。
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?