簡體   English   中英

需要幫助找出適用於我的數據結構的語法

[英]Need help finding out what syntax works with my data structure

我正在嘗試使用一系列鏈接列表來實現相鄰列表:

vector< list<Edge> > adjList;

目前,我無法編譯它,因為我不太確定如何訪問

頂點或什至是我嵌套類Edge的權重。 這是錯誤輸出...

Graph.cpp: In member function `void Graph::set_Edge(std::string, std::string, int)':
Graph.cpp:30: error: 'class std::list<Graph::Edge, std::allocator<Graph::Edge> >' has no member named 'm_vertex'
makefile.txt:9: recipe for target `Graph.o' failed
make: *** [Graph.o] Error 1

這是Graph.h文件中的類聲明(很抱歉,所有注釋,我想將所有想法留在代碼中,直到准備好將其打開為止)。

    #ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
//class MinPriority;
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
class Graph
{
public:
    Graph();
    ~Graph();
    /*void setArray(string vertex);
    void sortArray();
    Graph* getGraph(string preVertex, string vertex, int weight);
    void setGraph(string vertex, string postVertex, int weight);*/
    void set_Edge(string targetVertex, string vertex, int weight);
    friend class MinPriority;
private:
    class Edge
    {
    public:
        Edge(string vertex, int weight)
        {m_vertex = vertex; m_weight = weight;}
        ~Edge();
        string get_vertex(){}
        int get_weight(){}
    private:
        string m_vertex;
        int m_weight;
    };
    vector< list<Edge> > adjList;

};


#endif // GRAPH_H_INCLUDED

這是Graph.cpp

#include "Graph.h"

void Graph::set_Edge(string targetVertex, string vertex, int weight) //find target vertex
{
    for(unsigned int u = 0; u <= adjList.size(); u++)//traverses through the Y coord of the 2D array
    {
        if(targetVertex == adjList[u].m_vertex) // <<THIS IS WHERE THE ERROR OCCURS!!!!
        {
            adjList[u].push_back(Edge(vertex, weight)); //push new element to the back of the linked list at array index u.
        }

    }
    //we now need to add
    //adjList[adjList.size()].push_back(Edge(vertex, weight));
}

再次,我基本上需要幫助找出如何訪問Edge的各個部分(頂點或權重) if(targetVertex == adjList[u].m_vertex)是我希望使用的思想,它將在數組中找到'u'索引並檢查“ u”索引的頂點元素並將其與目標頂點進行比較。

adjList的類型為list<Edge> ,因此它沒有任何名為m_vertex成員。 它包含的節點具有m_vertex成員。

#include "Graph.h"

void Graph::set_Edge(string targetVertex, string vertex, int weight) 
{
    for(unsigned int u = 0; u <= adjList.size(); u++)
    {
        if(adjList[u].front().m_vertex == targetVertex)  
                   //^^add front() to access a list node 
                   //because the m_vertex member is same of all the edges in the list
                   //So just access the first one for simplicity.
        {
            adjList[u].push_back(Edge(vertex, weight)); 
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM