如何解决'对类型的引用需要初始化程序?'

问题描述 投票:0回答:1

我正在基于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) {
    }
};

/ Library / Developer / CommandLineTools / usr / include / c ++ / v1 / tuple:1360:7:错误:对类型'std :: __ 1 :: __ list_iterator'的引用需要初始化程序秒(_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,std :: __ 1 :: allocator&>>> :: operator []'在这里请求keys_to_data [key];

c++ c++11 unordered-map
1个回答
2
投票

您不能将参考作为值存储在地图中

© www.soinside.com 2019 - 2024. All rights reserved.