使用c++的boost库graph绘制一个网络图
以下是使用C++的Boost库的Graph模块绘制一个网络图的示例代码:
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
int main()
{
// 定义一个有向图类型
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> Graph;
// 创建一个图对象
Graph g;
// 添加顶点
boost::add_vertex(g);
boost::add_vertex(g);
boost::add_vertex(g);
// 添加有向边
boost::add_edge(0, 1, g);
boost::add_edge(1, 2, g);
boost::add_edge(2, 0, g);
// 创建一个输出流对象,用于输出GraphViz格式的图形描述
std::ofstream out("graph.dot");
// 使用GraphViz库将图写入输出流
boost::write_graphviz(out, g);
// 关闭输出流
out.close();
std::cout << "Graph created and saved as graph.dot" << std::endl;
return 0;
}
此代码创建了一个有向图,其中包含三个顶点和三条有向边。然后,使用GraphViz库将图形写入名为"graph.dot"的输出文件中。可以使用GraphViz的命令行工具将该文件转换为其他格式的图像,如PNG、SVG等
原文地址: https://www.cveoy.top/t/topic/ilWf 著作权归作者所有。请勿转载和采集!