调试地图插入?

发布于 2024-12-10 00:16:14 字数 605 浏览 1 评论 0原文

我在向地图中插入条目时遇到问题。

#include <stdio.h>
#include <vector>
#include <stack>
#include <map>

using namespace std;

class Nodo
{
public:
    vector<Nodo> Relaciones;
    int Valor;
    bool Visitado;

    Nodo(int V)
    {
        Valor = V;
        Visitado = false;
    }
};

class Grafo
{
public:
    Nodo *Raiz;
    map<int, Nodo> Nodos;

    Grafo(int V)
    {
        Raiz = new Nodo(V);
        //Getting http://msdn.microsoft.com/en-us/library/s5b150wd(v=VS.100).aspx here
        Nodos.insert(pair<int, Nodo>(V, Raiz));
    }
};

I'm having an issue with inserting an entry into a Map.

#include <stdio.h>
#include <vector>
#include <stack>
#include <map>

using namespace std;

class Nodo
{
public:
    vector<Nodo> Relaciones;
    int Valor;
    bool Visitado;

    Nodo(int V)
    {
        Valor = V;
        Visitado = false;
    }
};

class Grafo
{
public:
    Nodo *Raiz;
    map<int, Nodo> Nodos;

    Grafo(int V)
    {
        Raiz = new Nodo(V);
        //Getting http://msdn.microsoft.com/en-us/library/s5b150wd(v=VS.100).aspx here
        Nodos.insert(pair<int, Nodo>(V, Raiz));
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难以启齿的温柔 2024-12-17 00:16:14

您的类型不匹配。您将 Nodo* 传递到 pair 构造函数中,而它需要一个 Nodo 对象。

您声明:,

Nodo *Raiz;

然后尝试调用:,

pair<int, Nodo>(V, Raiz)

它需要一个 int 和一个 Nodo。但您传递了 intNodo*

您可能想要的是这样的:

class Grafo
{
    public:
        Nodo *Raiz;
        map<int, Nodo*> Nodos;    //  change to pointer

        Grafo(int V)
        {
            Raiz = new Nodo(V);
            //Getting http://msdn.microsoft.com/en-us/library/s5b150wd(v=VS.100).aspx here
            Nodos.insert(pair<int, Nodo*>(V, Raiz));   // change to pointer
        }
};

You have a type mismatch. You're passing a Nodo* into the pair constructor while it expects a Nodo object.

You declare:

Nodo *Raiz;

and then you try to call:

pair<int, Nodo>(V, Raiz)

which expects an int and a Nodo. But you passed it int and Nodo*.

What you probably want is this:

class Grafo
{
    public:
        Nodo *Raiz;
        map<int, Nodo*> Nodos;    //  change to pointer

        Grafo(int V)
        {
            Raiz = new Nodo(V);
            //Getting http://msdn.microsoft.com/en-us/library/s5b150wd(v=VS.100).aspx here
            Nodos.insert(pair<int, Nodo*>(V, Raiz));   // change to pointer
        }
};
薄情伤 2024-12-17 00:16:14

问题是 Rais 是一个指向 Nodo 的指针,但您试图将其插入到从 intNodo< 的映射中/code> (不是从 intNodo* 的映射)。

尝试:

class Grafo
{
    public:
        Nodo *Raiz;
        map<int, Nodo> Nodos;

        Grafo(int V)
        {
            Raiz = &*Nodos.insert(pair<int, Nodo>(V, Nodo(V))).first;
        }
};

The problem is that Rais is a pointer to Nodo, but you are trying to insert it into a map from int to Nodo (not a map from int to Nodo*).

Try:

class Grafo
{
    public:
        Nodo *Raiz;
        map<int, Nodo> Nodos;

        Grafo(int V)
        {
            Raiz = &*Nodos.insert(pair<int, Nodo>(V, Nodo(V))).first;
        }
};
听你说爱我 2024-12-17 00:16:14

如前所述,“new”返回指向该对象的指针。为了获取对象本身,您需要使用“*”运算符取消引用它。这就是地图无法工作的原因。

另外,如果您想将值插入到我个人认为看起来更清晰的地图中,可以这样做

typedef map<int, Nodo> MyMap;
MyMap myawesomemap;
int V = 5;
Nodo* Raiz = new Raiz(5);
myawesomemap.insert(MyMap::value_type(V, (*Raiz)));

As previously mentioned, 'new' returns a pointer to the object. In order to obtain the object itself, you would need to dereference it by using the '*' operator. That is why the map fails to work.

Additionally if you want to insert values into a map which I personally believe looks clearer is by doing

typedef map<int, Nodo> MyMap;
MyMap myawesomemap;
int V = 5;
Nodo* Raiz = new Raiz(5);
myawesomemap.insert(MyMap::value_type(V, (*Raiz)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文