简体   繁体   English

类值分割错误的映射

[英]map with class value segmentation fault

Design and implement a data structure for Least Recently Used (LRU) cache. 设计和实现最近最少使用(LRU)缓存的数据结构。 It should support the following operations: get and set. 它应该支持以下操作:获取和设置。

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. get(key)-如果键存在于缓存中,则获取键的值(始终为正),否则返回-1。 set(key, value) - Set or insert the value if the key is not already present. set(key,value)-如果密钥不存在,则设置或插入该值。 When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. 当缓存达到其容量时,它应在插入新项目之前使最近最少使用的项目无效。

#include <iostream>
#include <map>
using namespace std;
struct node{
    int val;
    struct node* next;
    struct node* prev; 
};
class dlist{
    public:
        dlist(){}
        dlist(int capacity){
            cap=capacity;
        }
        void add(int value){
            node* n=new node;
            n->val=value;
            if (size==0){
                size++;
                tail=n;
                head=tail;
            }
            else {
                if (size==cap){
                    node* buf=head;
                    head=head->next;
                    head->prev=NULL;
                    delete buf;
                    size--;
                }
                tail->next=n;
                n->prev=tail;
                tail=n;
                size++;
            }
        }
        int getVal(){
            if (tail==NULL)
                return -1;
            return tail->val;
        }
    private:
        int cap;
        int size;
        node* tail;
        node* head;
};

class LRUCache{
    public:
        LRUCache(int capacity) {
            cap=capacity;
        }
        int get(int key) {
            if(cap!=0&&cache.find(key)!=cache.end())
                return cache[key].getVal();
            return -1;
        }
        void set(int key, int value) {
            if (cap==0)
                return;
            if(cache.find(key)==cache.end()){
                dlist d=dlist(cap);
                cache.insert(make_pair(key,d));
            }
            cache[key].add(value);
        }
    private:
        int cap;
        map<int,dlist> cache;
};

int main()
{
   LRUCache lru(3);
                   cout<<"asd";
   lru.set(1,9);
   lru.set(1,8);
   lru.set(1,1);
   lru.set(1,7);
   lru.set(2,9);
    cout<<lru.get(1)<<endl;
    cout<<lru.get(2)<<endl;
    cout<<lru.get(3)<<endl;
   return 0;
}

so I used a map and a custom double linked list, it seems to working fine with if I add the cout line right after initializing LRU, but it will have seg fault if I don't, I and not very sure what should I do to manage the memory use of LRU(if this is the problem) 所以我使用了地图和自定义的双向链接列表,如果在初始化LRU之后立即添加cout行,它似乎可以正常工作,但是如果我不这样做,则会出现段错误,我也不知道该怎么办管理LRU的内存使用(如果这是问题)

Also if there's any line that could be better written(aside from std namespace) please tell me, I would really appreciate that. 另外,如果有什么行可以写得更好(除了std名称空间),请告诉我,我真的很感谢。

Your program exhibits undefined behavior since the member variables size , tail , and head of dlist are not initialized before being used. 由于成员变量sizetaildlist head在使用前未初始化,因此程序表现出未定义的行为。

Use 采用

dlist() : dlist(0) {}
dlist(int capacity) : cap(capacity), size(0), tail(nullptr), head(nullptr) {}

That fixes the segmentation violation problem in my testing. 这解决了我测试中的细分违规问题。

I recommend adding a constructor to node also: 我建议也向node添加一个构造函数:

struct node{

    node(int v) : val(v), next(nullptr), prev(nullptr) {}

    int val;
    struct node* next;
    struct node* prev; 
};

and use 和使用

node* n=new node(value);

instead of 代替

node* n=new node;
n->val=value;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM