【问题标题】:Creating graph in loop from Boost library c++从 Boost 库 c++ 循环创建图形
【发布时间】:2014-08-26 00:55:45
【问题描述】:

我正在尝试将自己从 R 转换为 C++,并且正在努力解决一个特定的图形问题。我有一个名为“Gra”的字符串矩阵,如下所示。

      int main(){
      string Gra[4][5] =   {{"V0", "V1", "V2", "V3", "V4"}, 
                            {"V5", "V6", "NA", "NA", "V7"},
                            {"V8", "V9", "NA", "NA", "V10"},
                            {"V11", "V12", "V13", "V14", "V15"}};

其中“V0”代表一个节点,而“NA”不是。这个矩阵来自一个叫做“base”的矩阵

   int base[4][5] = {{1, 1, 1, 1, 1}, 
                    {1, 1, 0, 0, 1},
                    {1, 1, 0, 0, 1},
                    {1, 1, 1, 1, 1}};


typedef float Weight;
typedef boost::property<boost::edge_weight_t, Weight> WeightProperty;
typedef boost::property<boost::vertex_name_t, std::string> NameProperty;

typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS,
NameProperty, WeightProperty > Graph;

typedef boost::graph_traits < Graph >::vertex_descriptor Vertex;

typedef boost::property_map < Graph, boost::vertex_index_t >::type IndexMap;
typedef boost::property_map < Graph, boost::vertex_name_t >::type NameMap;

typedef boost::iterator_property_map < Vertex*, IndexMap, Vertex, Vertex& > PredecessorMap;
typedef boost::iterator_property_map < Weight*, IndexMap, Weight, Weight& > DistanceMap;

 Graph g;

问题出在哪里,试图循环描述图形。我想将节点声明为

    Vertex V0 = boost::add_vertex(std::string("V0"), g);            // Struggling to implement this in a loop
    Vertex V1 = boost::add_vertex(std::string("V1"), g);
    Vertex V2 = boost::add_vertex(std::string("V2"), g);
    Vertex V3 = boost::add_vertex(std::string("V3"), g); 
    Vertex V4 = boost::add_vertex(std::string("V4"), g);
    Vertex V5 = boost::add_vertex(std::string("V5"), g);
    Vertex V6 = boost::add_vertex(std::string("V6"), g);
    Vertex V7 = boost::add_vertex(std::string("V7"), g); 
    Vertex V8 = boost::add_vertex(std::string("V8"), g);
    Vertex V9 = boost::add_vertex(std::string("V9"), g);
    Vertex V10 = boost::add_vertex(std::string("V10"), g);
    Vertex V11 = boost::add_vertex(std::string("V11"), g); 
    Vertex V12 = boost::add_vertex(std::string("V12"), g);
    Vertex V13 = boost::add_vertex(std::string("V13"), g);
    Vertex V14 = boost::add_vertex(std::string("V14"), g);
    Vertex V15 = boost::add_vertex(std::string("V15"), g); 

我试图通过这样的循环来复制它。

for ( int i=0; i < 4; i++)  // So this will run along all elements of our base vector
{       
  for ( int j=0; j < 5; j++)    // Length is the number of elements in our array
  {
  if( !(Gra[i][j] == "NA")) // Whilst going along each element inspecting whether it is a true node
  {
      Vertex Gra[i][j] = boost::add_vertex(std::Gra[i][j], g);  // This is where the problem is
  }
  }

}

所以问题来自于使用一个字符串来定义这个 Vertex 类的对象。任何机构可以帮助我吗?我很确定这是我正在努力解决的命名约定问题。如果解决了这个问题,那么我可以在创建边缘方面解决我的其余问题,我也有同样的问题,即尝试使用字符串调用类“顶点”的对象。

在此先感谢西里尔

【问题讨论】:

    标签: c++ loops boost graph


    【解决方案1】:

    表达式!Gra[i][j] == "NA" 不会按照您的预期进行。它会首先检查Gra[i][j]是否为“假”,然后将布尔结果与字符串"NA"进行比较。

    相反,要么在相等检查周围使用括号,要么进行不相等检查。所以要么

    !(Gra[i][j] == "NA")
    

    Gra[i][j] != "NA"
    

    还有一个问题是您在内循环中声明了一个局部数组变量Gra,这将导致与您的外部Gra 变量发生冲突。我想这就是你在那里使用std::Gra 的原因,但Gra 没有在标准命名空间中声明。你也不能使用::Gra,因为Gra也没有在全局命名空间中声明。

    不要在内循环内声明一个新变量,而是在循环外声明一个数组Vertexes

    Vertex Vertexes[4][5];
    

    然后使用该变量存储boost::add_vertex 的结果。

    【讨论】:

    • 太棒了,这些是我想念的东西。我可以将其更改为 !base[I][j] = 0
    • 我需要帮助的问题是 boost 想看到 - Vertex V15 = boost::add_vertex(std::string("V15"),g);但是我只能给它 Vertex "V15" = boost::add_vertex(std::string("V15"), g);
    • @Cyrillm_44 以字符串(例如"V15")为键的Vertex 对象中的hash table 怎么样?我的意思是,而不是我的答案中的 Vertexes 数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 2021-10-20
    • 2019-04-09
    相关资源
    最近更新 更多