没有匹配的成员函数可以调用' push_back'错误
在实施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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个迭代器变量,而不是
nullptr
值,如Bellow:Create an iterator variable, instead of
nullptr
value, as bellow: