Boost Dijkstra代码导致段内存损坏?

时间:2017-12-20 14:33:31

标签: c++ boost dijkstra

我正在尝试使用boost dijkstra算法在图表中找到最短路径。

std::pair<c_vertex_iterator_t, c_vertex_iterator_t> vi;
std::pair<c_vertex_iterator_t, c_vertex_iterator_t> vj;

boost::property_map<ConGraph,boost::edge_weight_t>::type weightmap = get(boost::edge_weight, cg);
std::vector<c_vertex_t> p(num_vertices(cg));
std::vector<int> d(num_vertices(cg));
for (vi = vertices(cg); vi.first != vi.second; ++vi.first)
{
    boost::dijkstra_shortest_paths(cg, *vi.first,
           predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, cg))).
           distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, cg))));
    for (vj = vertices(cg); vj.first != vj.second; ++vj.first)
    {
        distMat[*vi.first][*vj.first]= d[*vj.first];
    }
}

return boost::num_vertices(cg);

但我在这段代码中遇到了问题;应用程序停止在此行运行:

distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, cg))));

visual c ++检测由此指令引起的损坏的内存段错误

retval = HeapFree(_crtheap, 0, pBlock);

我该怎么做才能解决问题?

1 个答案:

答案 0 :(得分:0)

我正在使用@ 1201ProgramAlarm:为<()安全地分配内存表明代码几乎没有问题:

<强> Live On Coliru

python <(curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -s) --region eu-west-2

打印(例如,随机生成的图形):

distMat