小编Pau*_*aul的帖子

如何修复“对类型的引用需要初始值设定项”?

我正在基于 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 的实例化中, …

c++ unordered-map c++11

0
推荐指数
1
解决办法
2718
查看次数

标签 统计

c++ ×1

c++11 ×1

unordered-map ×1