创建一个使用Boost库从一个文件中的图表 [英] create graphs from a file using boost library
问题描述
我试图写一个程序,谁可以读取,并使用Boost库从一个文件中的图表。
首先,我先从一些吃茶。
该文件将是这样的: http://pastebin.com/g4cgaHJB
每次我找到像(T#1),其平均曲线与1号线。
(五0 12),其平均id为0和标签12顶点。
(E 1 3 52),其与ID为1和顶点ID为3顶点的标签52的平均优势。
I'm trying to write a program who can read and create graphs from a file using BOOST library. first, I start with some explications. the file will be something like: http://pastebin.com/g4cgaHJB every time I find a line like (t # 1) its mean graph with id 1. (v 0 12) its mean vertex with id 0 and label 12. (e 1 3 52) its mean edge with label 52 between vertex with id 1 and vertex with id 3.
我开始读取文件每次我找时间(t#..)我创建了一个图,我把它放在图表的向量。
这里是code:
I started with reading the file and every time I find(t # ..) I create a graph and I put it in a vector of graphs. here is the code:
#include <iostream>
#include <vector>
#include <ctime>
#include <set>
#include <fstream>
#include <string>
#include <unordered_set>
#include <cstdlib>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;
using namespace boost::algorithm;
/*********************************************/
//vertex
struct VertexProperties
{
int id;
int label;
VertexProperties(){}
VertexProperties(unsigned i, unsigned l) : id(i), label(l) {}
};
//edge
struct EdgeProperties
{
unsigned id;
unsigned label;
EdgeProperties(){}
EdgeProperties(unsigned i, unsigned l) : id(i), label(l) {}
};
//Graph
struct GraphProperties
{
unsigned id;
unsigned label;
GraphProperties() {}
GraphProperties(unsigned i, unsigned l) : id(i), label(l) {}
};
//adjency list
typedef boost::adjacency_list<
boost::vecS, boost::vecS, boost::directedS,
VertexProperties,
EdgeProperties,
GraphProperties
> Graph;
//descriptors
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
//iterators
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::edge_iterator edge_iter;
/****************************************************************************/
//le seuil int seuil;
std::vector<std::string> tokens;
/*
programme principal
*/
int main()
{
vector<Graph> dataG; // vector<graphes> * pointsdataG;
ifstream * file_reader= new ifstream("5.txt" ); //flux d'entrée pour opérer sur les fichiers.
while (!file_reader->eof())
{
string line;
file_reader->sync(); //Synchronise le tampon d'entree avec la source de donnees associee.
getline(*file_reader, line); //lire les caracteres a partir du flux d'entree (file_reader) et les place dans une chaine: (line)
if(line[0]=='t') // ligne de transaction(graphe)
{
split(tokens, line, is_any_of(" "));
int gid=atoi(tokens[2].c_str());
Graph g(GraphProperties(gid,gid));
cout<<gid<<endl;
dataG.push_back(g);
}
}
std::cout << "dataG contains:";
for (std::vector<Graph>::iterator it = dataG.begin() ; it != dataG.end(); ++it)
{
std::cout << ' ' << (*it)->label;
std::cout << '\n';
}
}
但我有一个问题,此行
but I have a problem with this line
std::cout << ' ' << (*it)->id;
解决这个之后,我将每一次我往前走时把code:)
After solving this I will put the code every time I go forward :)
推荐答案
首先,你的 myvector
没有定义或任何声明。
First of all, your myvector
isn't defined or declared anywhere.
其次,你说行,你有性病::法院℃的问题;&LT; ''&LT;&LT; (*它) - 方式&gt;的ID;
,不会在$ C $存在C
Secondly, the line you say you have a problem with std::cout << ' ' << (*it)->id;
, doesn't exist in your code.
然而,问题是最有可能同你真正的做的在你的code。您遍历包含的adjacency_list
项的载体,然后试图从图形对象的节点值。
However, the problem is most likely the same as what you actually do have in your code. You are looping over a vector containing adjacency_list
items, and then trying to extract a node value from the graph object.
您需要一个额外的内部循环,从节点中提取值。
You need an additional inner loop, to extract values from the nodes.
UPDATE1
这可能是这样的:
update1 This could be something like:
std::cout << "dataG contains:";
for (auto gr : dataG)
{
for (size_t i = 0 ; i < num_vertices(gr) ; ++i) {
std::cout << ' ' << gr[i].id << std::endl;
}
}
UPDATE2
您的意见搞糊涂了。如果你想要做的是填充的adjacency_list,那么这就是你怎么可以这样做:
Your comments have confused me. If what you wanted to do was to populate the adjacency_list, then this is how you could do it:
while (!file_reader->eof())
{
string line;
file_reader->sync();
getline(*file_reader, line);
std::stringstream ss(line);
char lineType;
ss >> lineType;
if(lineType =='t')
{
char dummy;
int gid;
ss >> dummy >> gid;
Graph g(GraphProperties(gid,gid));
dataG.push_back(g);
} else if (lineType == 'v') {
assert(!dataG.empty());
int vId, vLabel;
ss >> vId >> vLabel;
boost::add_vertex(VertexProperties(vId, vLabel), dataG[dataG.size()-1]);
} else if (lineType == 'e') {
assert(!dataG.empty());
int fromId, toId, vLabel;
ss >> fromId >> toId >> vLabel;
// Note that the EdgeProperty.id doesn't make sense with your input data
// as it only contains [vertexFrom vertexTo edgeData]
boost::add_edge(fromId, toId,
EdgeProperties(0, vLabel), dataG[dataG.size()-1]);
}
}
我相信你的'身份证'的使用是多余的顶点,因为他们都将附加有隐含的ID。但是,如果你仍然想有一个顶点属性,这很好。然而,你做边相同,但边缘条目数据不具有ID字段。
I believe your use of 'id' is redundant for vertices, as they will all have implicit id's attached to them. However, if you still want to have that as a vertex property, that is fine. However, you do the same for edges, but the edge entry data doesn't have an 'id' field.
UPDATE3 *
要访问和输出图形的内容:
To access and output the contents of the graphs:
typedef std::pair<edge_iter, edge_iter> edge_pair;
std::cout << "dataG contains:" << std::endl;
int c = 0;
for (auto gr : dataG)
{
++c;
std::cout << "Graph " << c << " contains " << num_vertices(gr) << " vertices, and " << num_edges(gr) << " edges" << std::endl;
std::cout << " Vertex list: " << std::endl;
for (size_t i = 0 ; i < num_vertices(gr) ; ++i) {
std::cout << " [" << i << "] ID: " << gr[i].id << ", Label: " << gr[i].label << std::endl;
}
std::cout << " Edge list: " << std::endl;
edge_pair ep;
for (ep = edges(gr); ep.first != ep.second; ++ep.first) {
vertex_t from = source(*ep.first, gr);
vertex_t to = target(*ep.first, gr);
edge_t edg = edge(from, to, gr);
std::cout << " [" << gr[from].id << "," << gr[to].id << "] ID: N/A, Label: " << gr[edg.first].label << std::endl;
}
std::cout << std::endl;
}
使用'5.txt'输出:
dataG contains:
Graph 1 contains 3 vertices, and 3 edges
Vertex list:
[0] ID: 0, Label: 0
[1] ID: 1, Label: 3
[2] ID: 2, Label: 9
Edge list:
[0,1] ID: N/A, Label: 10
[0,2] ID: N/A, Label: 4
[2,1] ID: N/A, Label: 68
Graph 2 contains 6 vertices, and 3 edges
Vertex list:
[0] ID: 0, Label: 2
[1] ID: 1, Label: 11
[2] ID: 2, Label: 6
[3] ID: 3, Label: 10
[4] ID: 4, Label: 18
[5] ID: 5, Label: 14
Edge list:
[0,1] ID: N/A, Label: 15
[1,3] ID: N/A, Label: 20
[2,5] ID: N/A, Label: 19
Graph 3 contains 2 vertices, and 1 edges
Vertex list:
[0] ID: 0, Label: 6
[1] ID: 1, Label: 11
Edge list:
[0,1] ID: N/A, Label: 13
Graph 4 contains 4 vertices, and 3 edges
Vertex list:
[0] ID: 0, Label: 2
[1] ID: 1, Label: 11
[2] ID: 2, Label: 19
[3] ID: 3, Label: 2
Edge list:
[0,1] ID: N/A, Label: 15
[0,3] ID: N/A, Label: 19
[1,2] ID: N/A, Label: 11
Graph 5 contains 3 vertices, and 4 edges
Vertex list:
[0] ID: 0, Label: 1
[1] ID: 1, Label: 16
[2] ID: 2, Label: 14
Edge list:
[0,1] ID: N/A, Label: 8
[0,2] ID: N/A, Label: 19
[0,2] ID: N/A, Label: 19
[1,2] ID: N/A, Label: 5
这篇关于创建一个使用Boost库从一个文件中的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!