C++ 具有字符串顶点的Boost图

C++ 具有字符串顶点的Boost图,c++,boost,graph,boost-graph,C++,Boost,Graph,Boost Graph,我知道如何在Boost Graph中创建具有整数或字符顶点的图形(请参见下面的注释代码)。问题是如何重写此代码以处理字符串顶点? #include <string> #include <boost/graph/adjacency_list.hpp> using namespace boost; int main (int argc, char **argv) { typedef adjacency_list <vecS, vecS, undire

我知道如何在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;
}
#包括
#包括
使用名称空间boost;
int main(int argc,字符**argv)
{
typedef邻接列表向量图;
//工作
//typedef std::对E;
//E边[]={E(2,5),E(5,3),E(3,1),E(5,1)};
//向量图(边[0],&边[0]+sizeof(边)/sizeof(E),4);
//工作
//typedef std::对E;
//E边[]={E('a','b')、E('a','c')、E('x','a')、E('b','x')};
//向量图(边[0],&边[0]+sizeof(边)/sizeof(E),4);
//不起作用
typedef std::对E;
E边[]={E(“aaa”、“bbb”)、E(“bbb”、“ccc”)、E(“aaa”、“xxx”)、E(“bbb”、“ccc”)};
向量图(边[0],&边[0]+sizeof(边)/sizeof(E),4);
返回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]
/usr/local/include/boost/graph/detail/adjacency_list.hpp:2076:错误:调用“add_edge(std::basic_string&,std::basic_string&,boost::adjacency_list&”)时没有匹配函数
/usr/local/include/boost/graph/detail/adjacency_list.hpp:1030:注:候选项为:std::pair boost::add_edge(typename配置::vertex_描述符,typename配置::vertex_描述符,boost::undirected_graph_helper&)[with Config=boost::detail::adj_list_gen::Config]

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

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

#包括
#包括
#包括
使用名称空间boost;
int main(int argc,字符**argv)
{
typedef属性VertexProperty;
typedef邻接列表向量图;
typedef std::对E;
E边[]={E(“aaa”、“bbb”)、E(“bbb”、“ccc”)、E(“aaa”、“xxx”)、E(“bbb”、“ccc”)};
常量字符*顶点[]={“aaa”、“bbb”、“ccc”、“xxx”};
地图索引;
const int nb_顶点=sizeof(顶点)/sizeof(顶点[0]);
//创建具有4个顶点的图形
向量图(nb_顶点);
//填充顶点的属性“顶点名称”
对于(int i=0;i
#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;
}