gpt4 book ai didi

c++ - 通过 boost 图将 vector 变量导出到图形

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:01 25 4
gpt4 key购买 nike

我想导出一个存储字符串值序列的 vector 以 boost 图形(点文件)。 .最后四行可以解释问题/所需的帮助。我知道代码有误,但需要指导方针来解决这个问题。我想将 vector toponodedist2 存储到图形点文件。这里我想存储一个数组/vector ,它将值序列存储在索引处,如 (image01、image02、iamge 03 ....)。稍后我将通过 write_graphviz_dp 将此 vector 导出到点文件。谢谢

#include<iostream>
#include<boost/graph/adjacency_list.hpp>
#include<boost/graph/graphviz.hpp>
#include<boost/graph/properties.hpp>
#include<boost/graph/property_maps/container_property_map.hpp>
#include<boost/graph/named_function_params.hpp>
#include <cstdlib>
#include<fstream>

struct VertexData
{

std:: string image_path;
int id;
int image_num;
std:: vector<std::vector<std::string>> toponodedist2;

};



int main(int,char*[])
{

VertexData v11;


std:: vector<std::vector<std::string>> toponodedist;
std:: vector<std::string> toponodedist1;
toponodedist1.push_back("this");
toponodedist1.push_back("This is first node2");
toponodedist1.push_back("This is first node3");
v11.toponodedist2.push_back(toponodedist1);
toponodedist.push_back(toponodedist1);
toponodedist1.clear();
toponodedist1.push_back("This is first node1");
toponodedist1.push_back("This is first node2");
toponodedist1.push_back("This is first node3");
toponodedist.push_back(toponodedist1);
v11.toponodedist2.push_back(toponodedist1);

for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
std::cout<< "this is "<<v11.toponodedist2[i][j]<<std::endl;

/// mention vertex data in declaring adjacency list
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexData,boost::no_property> MyGraphType;

MyGraphType G;
auto v1 =add_vertex(G);
auto v2 =add_vertex(G);
auto v3 =add_vertex(G);
auto v4 =add_vertex(G);
auto v5 =add_vertex(G);
auto v6 =add_vertex(G);
auto v7 =add_vertex(G);
auto v8 =add_vertex(G);
auto v9 =add_vertex(G);
auto e1 =add_edge(v1,v2,G);
auto e2 =add_edge(v2,v3,G);
auto e3 =add_edge(v3,v4,G);
auto e4 =add_edge(v4,v5,G);
auto e5 =add_edge(v5,v6,G);
auto e6 =add_edge(v7,v7,G);
auto e7 =add_edge(v5,v2,G);
auto e8 =add_edge(v1,v4,G);
auto e9 =add_edge(v3,v7,G);
auto e10 =add_edge(v2,v7,G);
auto e11 =add_edge(v2,v6,G);
auto vpair=vertices(G);
int numberOfInEdges = boost::out_degree(8,G);

std::cout<< "The number of vertices are "<<numberOfInEdges;
;

std::ofstream dotfile1;
dotfile1.open("dotgraph1.txt", std::ios::app);

boost::dynamic_properties dp;

dp.property("node_id", get(&VertexData::id, G));
// this is the place where I need help to export toponodeist2 to graph dot
// file ditfile1, but I am not able to do it. thanks
dp.property("path_Ismage", get(&VertexData::toponodedist2, G));
/// the line need to be addressed

boost::write_graphviz_dp(dotfile1,G,dp);
dotfile1.close();

return 0;
}

最佳答案

只需将属性转换为某种形式的字符串,例如:

dp.property("toponodedist", boost::make_transform_value_property_map(topo_attr, get(boost::vertex_index, g)));

例如

auto topo_attr = [&g](MyGraphType::vertex_descriptor const& v) {
auto& vd = g[v];
std::string s;
for (auto& row : vd.topo) {
for (auto& el : row) s += el + " ";
s += '\n';
}
return s;
};

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/transform_value_property_map.hpp>
#include <fstream>
#include <iostream>
#include <sstream>

using string_table = std::vector<std::vector<std::string>>;

struct VertexData {
std::string image_path;
int id;
int image_num;
string_table topo;
};

int main(int, char *[]) {

/// mention vertex data in declaring adjacency list
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexData>
MyGraphType;

MyGraphType g(9);
add_edge(0, 1, g);
add_edge(1, 2, g);
add_edge(2, 3, g);
add_edge(3, 4, g);
add_edge(4, 5, g);
add_edge(6, 6, g);
add_edge(4, 1, g);
add_edge(0, 3, g);
add_edge(2, 6, g);
add_edge(1, 6, g);
add_edge(1, 5, g);

std::srand(time(0));
auto gen_topo = []() -> string_table {
auto ranch = [] { return std::string(1, rand()%26 + 'a'); };
return {
{ ranch(), ranch(), ranch() },
{ ranch(), ranch(), ranch() },
{ ranch(), ranch(), ranch() },
};
};

for (auto v : boost::make_iterator_range(boost::vertices(g))) {
auto& data = g[v];
data.id = v;
data.topo = gen_topo();
}

std::cout << "out-degree: " << boost::out_degree(8, g);

{
std::ofstream dotfile;
dotfile.open("dotgraph.txt");

boost::dynamic_properties dp;

dp.property("node_id", get(&VertexData::id, g));

auto topo_attr = [&g](MyGraphType::vertex_descriptor const& v) {
auto& vd = g[v];
std::string s;
for (auto& row : vd.topo) {
for (auto& el : row) s += el + " ";
s += '\n';
}
return s;
};

dp.property("toponodedist", boost::make_transform_value_property_map(topo_attr, get(boost::vertex_index, g)));

boost::write_graphviz_dp(dotfile, g, dp);
}
}

点图看起来像:

graph G {
0 [toponodedist="q c i
z a e
o x c
"];
1 [toponodedist="i u t
c i l
s k w
"];
2 [toponodedist="z q m
q j l
n o t
"];
3 [toponodedist="u k q
g d u
o c v
"];
4 [toponodedist="u t s
w d n
r g x
"];
5 [toponodedist="e y h
b x z
n q i
"];
6 [toponodedist="a f y
w z j
m f o
"];
7 [toponodedist="g v s
d r l
w o p
"];
8 [toponodedist="l h x
i m x
s n v
"];
0--1 ;
1--2 ;
2--3 ;
3--4 ;
4--5 ;
6--6 ;
4--1 ;
0--3 ;
2--6 ;
1--6 ;
1--5 ;
}

演示可视化

只需使用 label 属性而不是 toponodedest 使其显示在标准点渲染上:

enter image description here

关于c++ - 通过 boost 图将 vector 变量导出到图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614317/

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