我想使用boost从文件中读取图形。输入文件中包含no.of边,然后是成对的边。我想打印邻接表。我尝试了以下代码。请帮我

    #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;
}


输入为

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

最佳答案

for循环中的int i=0, ei_next = ei;定义了一个新的局部int变量ei_next,因为在这种情况下,逗号不是逗号运算符,而是定义定界符。
由于eiedge_iterator,因此不能用于初始化int变量ei_next

ei_next = ei移动到for语句之前的行中(添加分号)以对其进行赋值。

关于c++ - 如何使用Boost从文件中读取图形?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37630688/

10-11 22:38
查看更多