我正在基于 LeetCode 练习实现 LRU 缓存,但以下代码无法编译
using namespace std;
class LRUCache {
private:
list<int> data;
unordered_map<int, list<int>::iterator&> keys_to_data;
void update_recency(int key, list<int>::iterator& it) {
data.erase(it);
data.push_front(key);
keys_to_data[key]; // issue here
}
public:
LRUCache(int capacity) {
}
int get(int key) {
int value = -1;
auto value_it = keys_to_data.find(key);
if(value_it != keys_to_data.end()) {
value = *(value_it->second);
update_recency(key, value_it->second);
}
return value;
}
void put(int key, int value) {
}
};
Run Code Online (Sandbox Code Playgroud)
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1360:7: 错误:对类型 'std::__1::__list_iterator' 的引用需要一个初始化器 Second(_VSTD::forward<_Args2>(_VSTD: :get<_I2>(__second_args))...) ^
...巨大的堆栈跟踪...
/Users/Paul/Desktop/int/main.cpp:17:21:注意:在成员函数 'std::__1::unordered_map &、std::__1::hash、std::__1::equal_to 的实例化中, …