Reputation:
I want to read a graph from a file using boost. the input file contains the no.of edges followed by the edges in pairs.I want to print the adjacency list.I tried the following code. please help me
#include <boost/graph/adjacency_list.hpp>
#include <fstream>
#include <boost/graph/graphviz.hpp>
using namespace std;
using namespace boost;
int main(){
typedef adjacency_list <listS, vecS, directedS> Graph;
typedef graph_traits<Graph>::vertex_descriptor index_vertex;
typedef boost::graph_traits <Graph>::edge_iterator edgeIt;
typedef boost::graph_traits<Graph>::vertex_iterator vertexIt;
// declare a graph object
Graph G;
std::ifstream infile("di.dat");
index_vertex n_vertices;
//int n_vertices;
if(infile >> n_vertices){
std::cout << n_vertices << endl; // read in number of vertices
}
while(infile)
{
int f;
int s;
infile >> f>> s;
// std::cout<<first<<second;
add_edge(f, s, G);
}
infile.close();
for(pair<vertexIt,vertexIt> vi = boost::vertices(G); vi.first != vi.second; ++vi.first) {
cout << *vi.first << endl;
}
for(pair<edgeIt,edgeIt> ei = boost::edges(G); ei.first != ei.second; ++ei.first) {
cout << source(*ei.first, G) << " -> " << target(*ei.first, G) << endl;
//cout << *ei.first << endl;
}
ofstream dotfile;
dotfile.open("test1.dot");
write_graphviz(dotfile, G);
system("xdot test1.dot");
return 0;
}
Input is
14
1 2
2 3
3 4
3 5
4 6
6 7
7 8
7 9
7 10
8 11
9 12
12 13
13 14
Upvotes: 0
Views: 651
Reputation: 3140
a basic example to show what's it going wrong is the following :
#include <iostream>
int main(int argc, char**argv)
{
char j;
for (int i=0, j='a'; j<'d'; i++, j++)
{
std::cout << j << std::endl;
}
std::cout << "++++++++++" << std::endl;
for (j='a'; j<'d';j++)
{
std::cout << j << std::endl;
}
return 0;
}
output :
97
98
99
++++++++++
a
b
c
this is a basic example of why this error is thrown. initialising an edge_iterator
with an int
. revisit your loop as @Marvin said.
Upvotes: 1
Reputation: 820
The int i=0, ei_next = ei;
in the for loop defines a new local int
variable ei_next
since the comma in this case is not the comma-operator but a definition delimiter.
And since ei
is an edge_iterator
it can not be used to initialize the int
variable ei_next
.
Move the ei_next = ei
in the line before the for
statement (adding a semicolon) to make it an assignment.
Upvotes: 0