Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指向结构的指针保留某些属性,同时将其他属性重置为零(使用map)_C++ - Fatal编程技术网

C++ 指向结构的指针保留某些属性,同时将其他属性重置为零(使用map)

C++ 指向结构的指针保留某些属性,同时将其他属性重置为零(使用map),c++,C++,我使用map来存储链接列表中指向节点的键和指针。 创建映射后,我使用MyMap.findkey将迭代器获取到所需的条目。使用MyItr->second检索指向节点的指针。 但当我访问存储在该节点上的值时,该值被设置为零 MyMap定义为 map<int,Node*> MyMap; //map the key to the node in the linked list 功能集如下所示: struct Node{ Node* next; Node* prev; int value;

我使用map来存储链接列表中指向节点的键和指针。 创建映射后,我使用MyMap.findkey将迭代器获取到所需的条目。使用MyItr->second检索指向节点的指针。 但当我访问存储在该节点上的值时,该值被设置为零

MyMap定义为

map<int,Node*> MyMap; //map the key to the node in the linked list
功能集如下所示:

struct Node{
Node* next;
Node* prev;
int value;
int key;
Node(Node* p, Node* n, int k, int val):prev(p),next(n),key(k),value(val){};
Node(int k, int val):prev(NULL),next(NULL),key(k),value(val){};
};
void set(int key,int value)
{

    if(counter<cp)
    {
        if(counter==0)
        {
            Node node(0,0,key,value);
            MyMap.insert(pair <int,Node*> (key,&node));
            head=&node;
            node.next=&node;
            node.prev=&node;
            prev_node=&node;
        }
        else
        {
            Node node(prev_node,head,key,value);
            MyMap.insert(pair <int,Node*> (key,&node));
            tail=&node;
            //tail=&node;
        }
        counter++;
    }
}
这是主要功能:

int main() {
int n, capacity,i; // capacity of Cache 
cin >> n >> capacity;
LRUCache l(capacity); // LRU is class contaning functions and map
for(i=0;i<n;i++) {
    string command;
    cin >> command;
    if(command == "get") {
        int key;
        cin >> key;
        cout << l.get(key) << endl;
    }
    else if(command == "set") {
        int key, value;
        cin >> key >> value;
        l.set(key,value);
    }
}
return 0;
}
Setkey,value函数创建链接列表的新节点,并使用该键在映射中添加指向该节点的指针

我知道双链接列表没有得到正确的实现,但我现在不关心这个问题,因为没有遍历该列表来获取值。 我哪里做错了

set函数存储指向堆栈上自动内存中实例化的节点变量的指针。当set退出时,变量超出范围,破坏节点对象并使映射中的指针悬空,因此当get尝试访问它们时,变量无效。您的代码具有未定义的行为

您需要更改集合以使用新操作符在堆上的动态内存中分配节点对象

void set(int key,int value)
{
    if(counter<cp)
    {
        if(counter==0)
        {
            Node *node = new Node(0,0,key,value); // <--
            MyMap.insert(std::make_pair(key,node));
            head=node;
            node->next=node;
            node->prev=node;
            prev_node=node;
        }
        else
        {
            Node *node = new Node(prev_node,head,key,value); // <--
            MyMap.insert(std::make_pair(key,node));
            tail=node;
        }
        counter++;
    }
}

如果使用C++ 11或更高版本,则可以考虑使用STD:::UnQuyQPTR或STD::SysDypPTR,其中STD::Wistar PtR,因为您正在创建一个链表,以便编译器在地图被破坏时处理对节点的破坏:

std::map<int,std::unique_ptr<Node>> MyMap;

void set(int key,int value)
{
    if(counter<cp)
    {
        if(counter==0)
        {
            std::unique_ptr<Node> node(new Node(0,0,key,value)); // <--
            MyMap.insert(std::make_pair(key,std::move(node)));
            head=node.get();
            node->next=head;
            node->prev=head;
            prev_node=head;
        }
        else
        {
            std::unique_ptr<Node> node(new Node(prev_node,head,key,value)); // <--
            MyMap.insert(std::make_pair(key,std::move(node)));
            tail=node.get();
        }
        counter++;
    }
}

int get(int k)
{
    auto Myitrerator = MyMap.find(k);
    if (Myitrator == MyMap.end())
        return -1; // return -1 if not found

    return Myitrator->second->value; // value to the corresponding key 
}

请你的问题包括一个即使在编辑后,它不是一个。我们仍然不知道mp被声明为什么,就这一点而言,您已经在未定义的LRUCache中引入了更多的未知数,它可能是定义get的类,并且可能定义mp,但我们不知道。将其减少到最小复制对我们和您都有好处,因为在将问题减少到最小复制的过程中,您会获得许多信息,这些信息可能有助于您自己发现问题。至少,请显示l.setkey,value后面的代码,可能它没有正确填充映射。还显示您在控制台上输入的输入值。@RemyLebeau我已经添加了所需的信息……我一直在想,如果我们可以进行其他分配,为什么我们需要新的。现在我知道了,非常感谢。还有很多东西要学。
for(std::map<int,Node*>::iterator iter = MyMap.begin(); iter != MyMap.end(); ++iter) {
    delete iter->second;
}
std::map<int,std::unique_ptr<Node>> MyMap;

void set(int key,int value)
{
    if(counter<cp)
    {
        if(counter==0)
        {
            std::unique_ptr<Node> node(new Node(0,0,key,value)); // <--
            MyMap.insert(std::make_pair(key,std::move(node)));
            head=node.get();
            node->next=head;
            node->prev=head;
            prev_node=head;
        }
        else
        {
            std::unique_ptr<Node> node(new Node(prev_node,head,key,value)); // <--
            MyMap.insert(std::make_pair(key,std::move(node)));
            tail=node.get();
        }
        counter++;
    }
}

int get(int k)
{
    auto Myitrerator = MyMap.find(k);
    if (Myitrator == MyMap.end())
        return -1; // return -1 if not found

    return Myitrator->second->value; // value to the corresponding key 
}