Невозможно удалить пару ключ-значение из stati c unordered_map - PullRequest
0 голосов
/ 15 апреля 2020

Этот код не удаляет пару ключ-значение из состояния c unordered_map. Я удалил элемент head из двусвязного списка из таблицы ha sh после вставки нового элемента. В чем проблема в коде?

#include<iostream>
#include<unordered_map>
using namespace std;

struct Node
{
    int key;
    int value;
    Node* next, * pre;
    Node(int key, int value)
    {
        this->key = key;
        this->value = value;
        next = pre = NULL;
    }

};
class something {
private:
    static unordered_map<int, Node*> hsmap;
    static Node* head, * tail;
public:
    static void set(int key, int value) {
        Node* temp = new Node(key, value);
        if (head == NULL) {
            head = tail = temp;
            hsmap[key] = temp;
        }
        else {
            tail->next = temp;
            temp->pre = tail;
            tail = temp;
            hsmap.erase(head->key);
            hsmap[key] = temp;
        }
    }
    static int get(int key) {
        if (hsmap.find(key) != hsmap.end())
            return hsmap[key]->value;
        return -1;
    }

};
//initializing static variables
unordered_map<int, Node*> temp;
Node* something::head = new Node(0, 0);
Node* something::tail = new Node(0, 0);
unordered_map<int, Node*> something::hsmap = temp;
int main() {
    something obj;
    obj.set(5, 4);
    obj.set(6, 7);
    cout << obj.get(5);
}
...