C++ 如何使用对象填充图形

C++ 如何使用对象填充图形,c++,C++,我有一个对象定义如下: class Item { long id; CString type; long pointingTo; }; std::list <Item> itemsFiltered; 我试着像这样填充图表: Graph g = ((long)itemsFiltered.size()); for(auto &it : itemsFiltered) { g.addEdge(it.id, it.pointingTo); } 但

我有一个对象定义如下:

class Item
{
    long id;
    CString type;
    long pointingTo;
};

std::list <Item> itemsFiltered;
我试着像这样填充图表:

Graph g = ((long)itemsFiltered.size());

for(auto &it : itemsFiltered)
{
    g.addEdge(it.id, it.pointingTo);
}
但这会崩溃(分割错误)。可能是因为我没有
adj
数组大小

我怎样才能解决这个问题?在这种情况下,填充图形的更好方法是什么

这是我使用的graph的实现

class Graph
{
    long V; // No. of vertices

    // Pointer to an array containing adjacency
    // lists
    std::list<long> *adj;
public:
    Graph(long V); // Constructor

    // function to add an edge to graph
    void addEdge(long v, long w);

    // prints BFS traversal from a given source s
    void BFS(long s, std::list<long> listOfIDs);
};

Graph::Graph(long V)
{
    this->V = V;
    adj = new std::list<long>[V];
}

void Graph::addEdge(long v, long w)
{
    adj[v].push_back(w); // Add w to v’s list.
}

void Graph::BFS(long s, std::list<long> listOfIDs)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;

    // Create a queue for BFS
    std::list<long> queue;

    // Mark the current node as visited and enqueue it
    visited[s] = true;
    queue.push_back(s);

    // 'i' will be used to get all adjacent
    // vertices of a vertex
    std::list<long>::iterator i;

    while(!queue.empty())
    {
        // Dequeue a vertex from queue and print it
        s = queue.front();
        listOfIDs.push_back(s);
        queue.pop_front();

        // Get all adjacent vertices of the dequeued
        // vertex s. If a adjacent has not been visited,
        // then mark it visited and enqueue it
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }
}
类图
{
长V;//顶点数
//指向包含邻接关系的数组的指针
//列表
标准::列表*adj;
公众:
图(长V);//构造函数
//函数将边添加到图形中
无效补遗(长v、长w);
//打印来自给定源的BFS遍历
无效BFS(长s,标准::列表ID);
};
图形::图形(长V)
{
这个->V=V;
adj=新标准::列表[V];
}
void图::addEdge(长v,长w)
{
adj[v]。推回(w);//将w添加到v的列表中。
}
空图::BFS(长s,标准::列表列表ID)
{
//将所有顶点标记为未访问
bool*visted=新bool[V];
对于(int i=0;i
使用较大的ID作为数组的标记,似乎正在访问超出范围的内容

如果要使用可由类型表示的任意值,则应使用(或者,如果您的环境支持该值)

要使用
std::map
,请替换

std::list<long> *adj;
应替换为

std::map<long, bool> visited;
std::访问地图;

避免超出范围的访问。

为什么要使用危险的原始指针?为什么不
std::vector adj?请发布一个。
itemsFiltered.size()
是否超过
10009
?您泄漏了分配的所有内容。简单的解决方法:不要自己编写
new
,而是使用stdlib容器或智能指针来正确处理释放。@MikeCAT
itemsFiltered.size()
包含元素。在这种情况下是10
10009
只是˙Item˙s.Hmmm之一的
长id
,您可能需要使用
std::map adj。我不确定您是否真的需要一个
映射
,您只需在其中插入
true
值。一个
std::set
就足够了。
std::map<long, std::list<long> > adj;
adj = new std::list<long>[V];
#include <map>
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
    visited[i] = false;
std::map<long, bool> visited;