1

Working on adjacency list --> directed weighted graph

One class looks like this, i.e. header:

class CGraph;
class CMap {
public:
    //voided constructors and destructors
    //functions one is:
    void SetDirGraph(string commands);

private:
   CGraph* m_myMap;
};

Second class:

class CNode {
public:
    //voided constructor and desctructor
    int m_distance, m_vert;
    bool m_isKnown;
};

typedef struct edges {
    int v2, weight;
} edge;

class CGraph {
public:
    CGraph(int map_size);
    ~CGraph(void);

    void AddMap(int v1, int v2, int weight);
    void AddEndVert(int v2, int weight);

private:
    list<edge> List;
    int size;

public:
    CNode* verts;
};

I'm reading vertices from a file, and that works. My problem is I am having trouble creating an adjacency list based on the code given. I'm trying to use pointers first that points to a list and it is not working correctly. I don't know how to create my pointers to the list without writing over them.

void CMap::SetDirGraph(string command) {
    istringstream buffer(command)
    char ch;
    int num, vert1, vert2, weight; //specify vertices and weight and number of vertices

    buffer>>ch;  //throw away first character (not needed)
    buffer>>num // size of vertices

    while(!buffer.eof()) {   // keep reading until end of line
        buffer>>v1;           // vertex  start
        buffer>>v2;          // vertex end
        buffer>>weight;

        m_myMap = new CGraph(map_size);  //initialize m_myMap.
        m_myMap->verts->m_vert = v1;  // mymap->verts->vert points to first edge
        m_myMap->AddMap(v1, v2, weight);  // create list?
        m_myMap->AddEndVert(v2, weight);  //create list? push v2 and weight on my list using my list.
    }
}

I've tried several different ways and I keep confusing myself, any point in the right direction would be awesome.

EDIT: I have more code too if needed to be produced, just publishing the main stuff. What I mean by "not working" is that I am just writing over the previous vertex. I don't know if I should create an array using m_myMap (tried and still writes over and get a memory error as well). No compiler errors.

4

1 回答 1

1

我不知道如何在不覆盖它们的情况下创建指向列表的指针。

除了您的应用程序之外,这个问题的答案是new运算符,我假设您知道,因为您在示例代码中使用了它。像这样的代码在堆上int * a = new int(42);分配内存int,你有责任在不再需要它时清理它。因此,您可以完全控制变量可用的时间。int x = 42; int * a = &x;另一方面,当x它超出范围时会自动清理,并且a将是一个指向不再有有意义数据的内存块的指针。如果你试图取消引用它,你会遇到未定义的行为,而且,如果你幸运的话,你的程序会崩溃。

如果您可以使用 C++11 标准或提供智能指针的库,那么您应该尽可能喜欢那些而不是自己管理指针。智能指针是一个对象,它保存分配的内存并在它被破坏时自动释放它。更具体的信息很大程度上取决于您使用的是哪种智能指针。使用智能指针的原因是自己进行管理既繁琐又容易出错。如果您不delete使用已分配的指针,您的应用程序将继续分配更多内存,直到有一天它崩溃(取决于分配的频率和内存量);这称为泄漏。如果您delete多次调用,您的程序也会退出。shared_ptr以下是您的应用程序中的 C++11 示例:

class CMap
{
  private:
    std::shared_ptr<CGraph> m_myMap;
  // etc.
};

// in SetDirGraph
m_myMap.reset(          // if the smart pointer has previously been managing
                        // memory, it will free it before allocating new
  new CGraph(map_size)  // allocate CGraph as before
);

除此之外,希望能回答您的问题的是,我遇到了与您的代码有关的几个潜在问题:

  • 绝对错误:在SetDirGraph你设置m_myMap->verts->m_vert = v1m_myMap->verts是一个指针。您刚刚创建m_myMap,因此verts未初始化,因此指向随机的内存块。然后,您尝试通过取消引用它m_myMap->verts->m_vert = v1。这行不通。您需要先创建verts,即verts = new CNode;.

  • typedef struct edges { /* etc */ } edge;是一个 C 构造,在 C++ 中不需要typedef包装器。它确实有效,但它确实是多余的,许多这些结构只会污染你正在工作的命名空间。

  • 你真的需要指针吗?您提供的片段没有暗示您为什么需要使用它们。您将希望将指针的使用减少到最低限度(或至少使用智能指针,见上文)

于 2012-06-08T06:30:00.623 回答