dimanche 18 août 2019

list1.erase( hash1.find ( p) ); no matching function to call 'erase' c++

Why doesn't it accept hash1.find(p) but hash1[p] works find if i use that?

class LRUCACHE {
    list<int> list1;   // store keys of Cache.

    unordered_map<int, list<int>::const_iterator> hash1;     // Store reference of Keys in Cache.
    int maxsize;

public:
    LRUCACHE(int);
    void set(int);
    void get(int);
};

LRUCACHE::LRUCACHE(int n) {    
    maxsize = n;
}


void LRUCACHE::get(int x) {
    // Not Found in Cache.
    if (hash1.find(x) == hash1.end() ) {

        // if Max size.
        if (list1.size() == maxsize ) {

            // returns the last element of LIST.
            int last = list1.back();

            // Remove last element of LIST and reduce size by 1.
            list1.pop_back();

            hash1.erase(last); 
        }
    }
    else {
        list1.erase(hash1[x]);
    }
    list1.push_front(x);
    hash1[x] = list1.begin(); // points and updates.
}

void LRUCACHE::get(int p) {
    if (hash1.find(p) == hash1.end() ){
        cout << "not found " << endl;
    }
    else {
        list1.erase(hash1.find(p)); // Error Here member function not found
    }
}


I think im using const_iterator for unordermap? so it should accept it list function call of iterator erase( const_iterator position ); ?

i think hash1.find should return const_iterator?

Aucun commentaire:

Enregistrer un commentaire