【问题标题】:A Boost graph with string vertexes具有字符串顶点的 Boost 图
【发布时间】:2013-09-16 07:54:39
【问题描述】:

我知道如何在 Boost Graph 中创建带有整数或字符顶点的图(请参阅下面的注释代码)。问题是如何重写此代码以使用字符串顶点?

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

using namespace boost;

int main (int argc, char **argv)
{
        typedef adjacency_list <vecS, vecS, undirectedS> vector_graph_t;

        //works
        //typedef std::pair <int, int> E;
        //E edges[] = { E (2, 5), E (5, 3), E (3, 1), E (5, 1)};
        //vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 4);

        //works
        //typedef std::pair <char, char> E;
        //E edges[] = { E ('a', 'b'), E ('a', 'c'), E ('x', 'a'), E ('b', 'x')};
        //vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 4);

        //does not work
        typedef std::pair <std::string, std::string> E;
        E edges[] = { E ("aaa", "bbb"), E ("bbb", "ccc"), E ("aaa", "xxx"), E ("bbb", "ccc")};
        vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 4); 

        return 0;
}

上面的程序编译出错:

/usr/local/include/boost/graph/detail/adjacency_list.hpp:2076: error: no matching function for call to ‘add_edge(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>&)’
/usr/local/include/boost/graph/detail/adjacency_list.hpp:1030: note: candidates are: std::pair<typename Config::edge_descriptor, bool> boost::add_edge(typename Config::vertex_descriptor, typename Config::vertex_descriptor, boost::undirected_graph_helper<C>&) [with Config = boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>::config]

【问题讨论】:

    标签: c++ boost graph boost-graph


    【解决方案1】:

    我认为它适用于 int 和 char 因为这两种类型用作顶点索引。但是,在您注释的代码中,您将从顶点 8 开始的边添加到包含 4 个顶点的图中...

    显式方法可能会给你正确的结果。如果您希望顶点属性由图形本身存储,则应声明该属性并将其链接到图形类型。您还应该使用它们的索引访问顶点,这不能直接从它们的属性中完成(不同的顶点可能具有相同的属性)。

    #include <string>
    #include <map>
    #include <boost/graph/adjacency_list.hpp>
    
    using namespace boost;
    
    int main (int argc, char **argv)
    {
      typedef property<vertex_name_t, std::string> VertexProperty;
    
      typedef adjacency_list <vecS, vecS, undirectedS, VertexProperty> vector_graph_t;
    
      typedef std::pair <std::string, std::string> E;
      E edges[] = { E ("aaa", "bbb"), E ("bbb", "ccc"), E ("aaa", "xxx"), E ("bbb", "ccc")};
    
      const char* vertices[] = {"aaa", "bbb", "ccc", "xxx"};
      std::map<std::string, vector_graph_t::vertex_descriptor> indexes;
    
      const int nb_vertices = sizeof(vertices)/sizeof(vertices[0]);
    
      // creates a graph with 4 vertices
      vector_graph_t g (nb_vertices); 
    
      // fills the property 'vertex_name_t' of the vertices
      for(int i = 0; i < nb_vertices; i++)
      {
        boost::put(vertex_name_t(), g, i, vertices[i]); // set the property of a vertex
        indexes[vertices[i]] = boost::vertex(i, g);     // retrives the associated vertex descriptor
      }
    
      // adds the edges
      // indexes[edges[0].first] maps "aaa" to the associated vertex index
      for(int i = 0; i < sizeof(edges)/sizeof(edges[0]); i++)
      {
        boost::add_edge(indexes[edges[i].first], indexes[edges[i].second], g);
      }
    
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多