【发布时间】:2015-02-28 09:22:07
【问题描述】:
我正在尝试编写一个程序,该程序可以使用 BOOST 库从文件中读取和创建图形。 首先,我从一些解释开始。 该文件将类似于:http://pastebin.com/g4cgaHJB 每次我找到像 (t # 1) 这样的线时,它的平均图 id 为 1。 (v 0 12) id 为 0 且标签为 12 的平均顶点。 (e 1 3 52) 在 id 为 1 的顶点和 id 为 3 的顶点之间带有标签 52 的平均边。
我从读取文件开始,每次找到(t # ..) 时,我都会创建一个图形并将其放入图形向量中。 这是代码:
#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';
}
}
但我对这条线有问题
std::cout << ' ' << (*it)->id;
解决这个问题后,我每次前进都会放代码:)
【问题讨论】: