簡體   English   中英

如何使用 boost make_label_writer 寫入邊緣屬性?

[英]How to use boost make_label_writer to write edge properties?

我有一個簡單的圖形,我成功地用頂點寫入屬性,但是當我使用make_label_writer將屬性寫入邊緣時,編譯器總是抱怨。 有人可以幫忙嗎?

我的代碼如下:

int main (int argc, char * argv[]) {
    typedef std::pair<int ,int> Edge;
    std::vector<Edge> used_by = {Edge(1, 0), Edge(2, 1),
            Edge(1,2), Edge(2, 0)};
    using namespace boost;
    typedef adjacency_list<vecS, vecS, directedS
             > Graph;
    Graph g(used_by.begin(), used_by.end(), 3);
    std::ofstream dmp;
    dmp.open("dmp.dot");
    //name for vertex
    std::vector<std::string> name{"one", "two", "three"};
    //name for edge
    std::vector<std::string> name1{"e1", "e2", "e3", "e4"};
    write_graphviz(std::cout, g, make_label_writer(&name[0]) 
                                   ,make_label_writer(&name1[0]));
}

write_graphviz()將調用模板,這非常好:

  template <typename Graph, typename VertexWriter, typename 
  EdgeWriter>
  inline void
  write_graphviz(std::ostream& out, const Graph& g,
             VertexWriter vw, EdgeWriter ew
       BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  {
    default_writer gw;
    write_graphviz(out, g, vw, ew, gw);
  }

所以現在的問題是:當我只使用make_label_writer(&name[0]]])編寫頂點屬性時,代碼運行完美。 但是當我添加make_label_writer(&name1[0]) ,出現錯誤。

默認頂點索引是整數,這就是為什么您可以使用第一個頂點名稱的地址作為隱含關聯屬性映射的原因。

邊緣描述符是一個不同的野獸,需要你

  • 創建一個顯式迭代器屬性映射(使用額外的索引屬性映射從邊緣描述符映射到整數索引到name1向量)
  • 或使用關聯 PropertyMap 概念的模型。

在這種情況下,您應該使用std::map<edge_descriptor, std::string>屬性操作。

還請考慮使用Bundled Properties使您的屬性生活更簡單。

關聯屬性映射

住在 Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> Graph;

int main() {
    Graph g(3);
    auto e1 = add_edge(1, 0, g).first;
    auto e2 = add_edge(2, 1, g).first;
    auto e3 = add_edge(1, 2, g).first;
    auto e4 = add_edge(2, 0, g).first;

    std::vector<std::string>                      vname{ "one", "two", "three" };
    std::map<Graph::edge_descriptor, std::string> ename{ 
        { e1, "e1" },
        { e2, "e2" },
        { e3, "e3" },
        { e4, "e4" },
    };

    write_graphviz(std::cout, g,
            boost::make_label_writer(&vname[0]),
            make_label_writer(boost::make_assoc_property_map(ename)));
}

印刷

改為捆綁屬性

住在 Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexProps { std::string name; };
struct EdgeProps   { std::string name; };
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertexProps, EdgeProps> Graph;

int main() {
    Graph g(3);
    g[0].name = "one";
    g[1].name = "two";
    g[2].name = "three";
    add_edge(1, 0, {"e1"}, g);
    add_edge(2, 1, {"e2"}, g);
    add_edge(1, 2, {"e3"}, g);
    add_edge(2, 0, {"e4"}, g);

    write_graphviz(std::cout, g,
            make_label_writer(get(&VertexProps::name, g)),
            make_label_writer(get(&EdgeProps::name, g)));
}

打印相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM