gpt4 book ai didi

c++ - 将 BOOST::Graph 复制到 std::vector

转载 作者:行者123 更新时间:2023-11-28 02:41:23 33 4
gpt4 key购买 nike

我必须使用 BOOST。将 BOOST::Graph 复制到 std::vector 的哪种方法更简单?

我想做这样的事情:

1. Create std::vector<float> v
2. for every vertex in the boost::graph g
v.push_back(g.getDataofCurrentVertex());

我已阅读 this链接,但我仍然无法弄清楚。

我希望将图形视为一个容器。

最佳答案

为了遍历 DAG,您可以使用深度优先或广度优先遍历,后者需要一个临时队列。您可以阅读有关这些算法的更多信息 herehere .

当然,BOOST 图数据结构为您实现了顶点和边迭代器……您可以使用他们在“访问您指向的链接的“顶点集”部分。简单遍历所有顶点的示例代码在下面的代码中:

int main(int,char*[])
{
// ...

// get the property map for vertex indices
typedef property_map<Graph, vertex_index_t>::type IndexMap;
IndexMap index = get(vertex_index, g);

std::cout << "vertices(g) = ";
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
std::pair<vertex_iter, vertex_iter> vp;
for (vp = vertices(g); vp.first != vp.second; ++vp.first) {
//***replace this code with your vector::push_back() code ***
std::cout << index[*vp.first] << " ";
}
std::cout << std::endl;
// ...
return 0;
}

关于c++ - 将 BOOST::Graph 复制到 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25871367/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com