没有匹配的成员函数可以调用' push_back'错误

发布于 2025-02-03 22:16:38 字数 1261 浏览 2 评论 0原文

在实施LRU缓存时,会有此错误。 早些时候,我正在通过地图实施它,但是它可以正常工作,但是即使以矢量为单位,它也无法正常工作。

#include <list>
class LRUCache {
    list<pair<int,int>> lru;
    int cap;
    vector<list<pair<int, int>>::iterator> hash;
public:
LRUCache(int capacity) {
    cap = capacity;
    for(int i=0;i<=3000;i++)
        hash.push_back(nullptr);
}

int get(int key) {
    if(hash[(key)]!=nullptr)
    {
        int v = hash[key]->first;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
        return v;
    }
    else
        return -1;
}

void put(int key, int value) {
    if(hash[(key)]!=nullptr)
    {
        int v = value;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
    }
    else if(lru.size()<cap)
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
    }
    else
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
        auto it = lru.end();
        it--;
        hash[(it->second)] = nullptr;
        lru.erase(it);
    }
}
};

这样也不起作用。

vector<list<pair<int, int>>::iterator> hash(3001,NULL);

我们不能创建指针的向量吗?

While implementing LRU cache got this error.
Earlier I was implementing it via maps it works then but somehow even when doing it as vector it does not work.

#include <list>
class LRUCache {
    list<pair<int,int>> lru;
    int cap;
    vector<list<pair<int, int>>::iterator> hash;
public:
LRUCache(int capacity) {
    cap = capacity;
    for(int i=0;i<=3000;i++)
        hash.push_back(nullptr);
}

int get(int key) {
    if(hash[(key)]!=nullptr)
    {
        int v = hash[key]->first;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
        return v;
    }
    else
        return -1;
}

void put(int key, int value) {
    if(hash[(key)]!=nullptr)
    {
        int v = value;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
    }
    else if(lru.size()<cap)
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
    }
    else
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
        auto it = lru.end();
        it--;
        hash[(it->second)] = nullptr;
        lru.erase(it);
    }
}
};

This way does not work either.

vector<list<pair<int, int>>::iterator> hash(3001,NULL);

Can we not create a vector of pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2025-02-10 22:16:38

创建一个迭代器变量,而不是nullptr值,如Bellow:

list<pair<int, int>>::iterator emptyIt;  // this iterator object refer to nothing

// Using `emptyIt` to initialize the hash
LRUCache(int capacity) {
    cap = capacity;
    for(int i=0;i<=3000;i++)
        hash.push_back(emptyIt);
}

// Using emptyIt instead of nullptr
int get(int key) {
    if(hash[(key)]!=emptyIt)
    {
        int v = hash[key]->first;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
        return v;
    }
    else
        return -1;
}

void put(int key, int value) {
    if(hash[(key)]!=emptyIt)
    {
        int v = value;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
    }
    else if(lru.size()<cap)
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
    }
    else
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
        auto it = lru.end();
        it--;
        hash[(it->second)] = emptyIt;
        lru.erase(it);
    }
}
};

Create an iterator variable, instead of nullptr value, as bellow:

list<pair<int, int>>::iterator emptyIt;  // this iterator object refer to nothing

// Using `emptyIt` to initialize the hash
LRUCache(int capacity) {
    cap = capacity;
    for(int i=0;i<=3000;i++)
        hash.push_back(emptyIt);
}

// Using emptyIt instead of nullptr
int get(int key) {
    if(hash[(key)]!=emptyIt)
    {
        int v = hash[key]->first;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
        return v;
    }
    else
        return -1;
}

void put(int key, int value) {
    if(hash[(key)]!=emptyIt)
    {
        int v = value;
        lru.erase(hash[key]);
        lru.push_front({v,key});
        hash[key] = lru.begin();
    }
    else if(lru.size()<cap)
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
    }
    else
    {
        lru.push_front({value,key});
        hash[key] = lru.begin();
        auto it = lru.end();
        it--;
        hash[(it->second)] = emptyIt;
        lru.erase(it);
    }
}
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文