问题描述
此处的最小可复制示例:https://repl.it/@amejiarosario/lrc-cachecpp
我正在使用unordered_map
和双向链接list
在C ++上实现LRU缓存。每个链接列表节点都是一个数组[key,value]
。 HashMap以整数作为键,值是指向链表中Node的指针/迭代器:
class LRUCache {
int max;
list<vector<int,int>> kvList;
unordered_map<int,decltype(kvList)::iterator> lmap;
public:
LRUCache(int capacity): max(capacity) {}
int get(int key) {
auto it = lmap.find(key);
if (it == lmap.end()) return -1;
put(key,(*it->second)[0]);
return (*it->second)[0];
}
void put(int key,int value) {
kvList.push_back({ key,value });
if (lmap.find(key) != lmap.end()) kvList.erase(lmap.at(key));
lmap[key] = prev(kvList.end());
}
};
运行此代码时遇到一些问题。当我像初始化
LRUCache *lRUCache = new LRUCache(2);
我收到以下错误:
clang++-7 -pthread -std=c++17 -o main main.cpp
In file included from main.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/list:63:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_list.h:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/ext/alloc_traits.h:49:47: error:
type 'int' cannot be used prior to '::' because it has no members
template<typename _Alloc,typename = typename _Alloc::value_type>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:83:35: note:
in instantiation of default argument for '__alloc_traits<int>' required
here
typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:339:30: note:
in instantiation of template class 'std::_Vector_base<int,int>'
requested here
class vector : protected _Vector_base<_Tp,_Alloc>
^
main.cpp:16:12: note: in instantiation of template class
'std::vector<int,int>' requested here
if (it == lmap.end()) return -1;
if (it == lmap.end()) return -1;
怎么了?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)