C++ LRU缓存实践上的运行时错误(C+;+;)

C++ LRU缓存实践上的运行时错误(C+;+;),c++,caching,C++,Caching,因此,我正在处理Leetcode上的LRU缓存问题,并试图在他们的站点上提交我的解决方案,结果出现运行时错误,并显示以下消息: 运行时错误 最后执行的输入: 1101、[set(253668)、set(202206)、set(12313177)、get(465)、get(1333)、set(6513249)、set(4532472)、get(1050)、set(145881)、set(12561320)、set(3421528)、get(37) 问题陈述是: 为最近最少使用(LRU)缓存设计并实

因此,我正在处理Leetcode上的LRU缓存问题,并试图在他们的站点上提交我的解决方案,结果出现运行时错误,并显示以下消息:

运行时错误 最后执行的输入: 1101、[set(253668)、set(202206)、set(12313177)、get(465)、get(1333)、set(6513249)、set(4532472)、get(1050)、set(145881)、set(12561320)、set(3421528)、get(37)

问题陈述是: 为最近最少使用(LRU)缓存设计并实现数据结构。它应支持以下操作:获取和设置

get(key)-如果缓存中存在密钥,则获取该密钥的值(将始终为正值),否则返回-1。 set(key,value)-如果密钥不存在,则设置或插入该值。当缓存达到其容量时,它应在插入新项之前使最近使用最少的项无效

我使用Xcode来测试我的代码,使用3个LRU容量,它给了我预期的行为,我没有这个运行时错误。如果你能帮助我理解我的错误所在,我将不胜感激。提前非常感谢

#include <iostream>
#include <map>
#include <vector>
using namespace std;

class listNode {
public:
    listNode *prev;
    listNode *next;
    int key;
    int data;
};

class LRUCache{
public:
    LRUCache(int capacity) {
       this->capacity = capacity;
       this->currentSize = 0;
       tail = NULL;
       head = NULL;
    }

    int get(int key) {
        // If you can find the key
        if(cache.find(key) != cache.end()) {
            // Mark it as most recently used
            head->prev = cache[key];
            head = cache[key];
            cache[key]->prev = NULL;

            return cache[key]->data;

        }
        else {
             return -1;
        }
     }

     void set(int key, int value) {
     // If you can find the key
     if(cache.find(key) != cache.end()) {
         // Change the value
         cache[key]->data = value;
         return;
     }
     else {
         currentSize ++;

         // Create a new node and insert at the tail
         if (currentSize == 1) {
             listNode *node = new listNode();
             node->key = key;
             node->data = value;
             cache[key] = node;
             head = node;
             head->prev = NULL;
             head->next = NULL;
             tail = head;

             // No point in having a LRU cache with a maxSize of 0 so we're OK
         }
         else {

             listNode *node = new listNode();
             node->data = value;
             node->key = key;
             node->prev = tail;
             tail->next = node;
             node->next = NULL;
             tail = node;
             cache[key] = node;


             // Remove the most recently used item if we are at capacity
             if(currentSize > capacity) {
                 listNode *trash = head;
                 head = head->next;

                 cache.erase(trash->key);
                 delete trash;
             }
         }
         return;
     }
 }
private:
    int capacity;
    int currentSize;
    map<int, listNode*> cache;
    int mostRecentlyUsedKey;
    listNode *head; // Head is the most recently used
    listNode *tail;
};
#包括
#包括
#包括
使用名称空间std;
类listNode{
公众:
listNode*prev;
listNode*下一步;
int键;
int数据;
};
类LRUCache{
公众:
LRUCache(内部容量){
这->容量=容量;
此->当前大小=0;
tail=NULL;
head=NULL;
}
int get(int键){
//如果你能找到钥匙
if(cache.find(key)!=cache.end()){
//将其标记为最近使用的
head->prev=缓存[键];
head=缓存[键];
cache[key]->prev=NULL;
返回缓存[key]->数据;
}
否则{
返回-1;
}
}
无效集(int键,int值){
//如果你能找到钥匙
if(cache.find(key)!=cache.end()){
//更改值
缓存[键]->数据=值;
返回;
}
否则{
currentSize++;
//创建新节点并在尾部插入
如果(currentSize==1){
listNode*node=新建listNode();
节点->键=键;
节点->数据=值;
cache[key]=节点;
头部=节点;
head->prev=NULL;
head->next=NULL;
尾=头;
//拥有maxSize为0的LRU缓存没有意义,所以我们可以
}
否则{
listNode*node=新建listNode();
节点->数据=值;
节点->键=键;
节点->上一个=尾部;
tail->next=节点;
节点->下一步=空;
尾=节点;
cache[key]=节点;
//如果我们已满,请删除最近使用的项目
如果(当前大小>容量){
listNode*垃圾=头部;
头部=头部->下一步;
缓存.擦除(垃圾->键);
删除垃圾;
}
}
返回;
}
}
私人:
国际能力;
int-currentSize;
地图缓存;
内特莫斯特利乌塞德基;
listNode*head;//head是最近使用的
listNode*尾部;
};

我们可以有一个
main()
吗?遗憾的是,Leetcode有自己的测试代码,我假设它使用这个类。