简体   繁体   English

没有匹配的成员函数调用“push_back”错误

[英]No matching member function to call for 'push_back' error

While implementing LRU cache got this error.在实现 LRU 缓存时出现此错误。 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?我们不能创建一个指针向量吗?

Create an iterator variable, instead of nullptr value, as bellow:创建一个迭代器变量,而不是nullptr值,如下所示:

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);
    }
}
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有匹配的成员函数来调用'push_back' - Error: no matching member function for call to 'push_back' C++ - 没有匹配成员 function 用于调用“push_back” - C++ - No matching member function for call to 'push_back' 没有匹配的成员 function 调用“push_back”,共享指针向量 - No matching member function call to 'push_back', vector of shared pointers C ++向量用法中的错误:没有匹配的成员函数可调用“ push_back” - Error in C++ Vector Usage: No matching member function for call to 'push_back' C++ 没有匹配成员 function 用于调用向量的“push_back”错误 - C++ No matching member function for call to 'push_back' error for vector 错误:没有用于调用 vector::push_back 的匹配函数 - error: no matching function for call to vector::push_back C++ 模板 class:没有匹配成员 function 用于调用“push_back” - C++ template class: No matching member function for call to 'push_back' C ++中的复合错误[没有匹配的成员函数来调用&#39;push_back&#39;] - Composite in C++ error [no matching member function to callto 'push_back'] 错误:- main.cpp:11:19: 错误: push_back 行中没有匹配的调用函数 - error:- main.cpp:11:19: error: no matching function for call in the push_back line “错误:没有匹配的函数来调用&#39;push_back(char [6])”。 如何解决此代码中的错误? - “error: no matching function for call to ‘push_back(char [6])”. How to solve the error in this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM