gpt4 book ai didi

c++ - 当顶点具有属性时,write_graphviz 不起作用

转载 作者:行者123 更新时间:2023-11-30 04:43:06 25 4
gpt4 key购买 nike

我正在学习 boost::graph 并尝试将 name 属性添加到 Vertex。然而,这样做之后,write_graphviz 不会保存任何东西(没有这个属性,它可以工作)。

头文件:

struct VertexProps
{
std::string name;
};

struct EdgeProps
{
double weight;
};

using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProps, EdgeProps>;
using Vertex = boost::adjacency_list<>::vertex_descriptor;
using Edge = std::pair<boost::adjacency_list<>::edge_descriptor, bool>;
using EdgeList = std::pair<boost::adjacency_list<>::edge_iterator,
boost::adjacency_list<>::edge_iterator>;

Cpp文件:

Vertex vertex = boost::add_vertex({std::move(vertexName)}, g);

std::filebuf fb;
fb.open("output.txt", std::ios::out);
std::ostream os(&fb);
write_graphviz(os, g, boost::make_label_writer(get(&VertexProps::name, g)), boost::make_label_writer(get(&EdgeProps::weight, g)));
fb.close();

文件应该包含图表,但我只能看到:

digraph G {
}

最佳答案

不清楚你有什么不同,但这是一个基于上述内容的独立示例:

Live On Wandbox

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexProps { std::string name; };
struct EdgeProps { double weight; };

using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProps, EdgeProps>;

int main(){
Graph g;

auto vertex = add_vertex({"MyVertexName"}, g);

write_graphviz(std::cout, g,
make_label_writer(get(&VertexProps::name, g)),
make_label_writer(get(&EdgeProps::weight, g)));
}

哪个打印

digraph G {
0[label=MyVertexName];
}

关于c++ - 当顶点具有属性时,write_graphviz 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58471489/

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