繁体   English   中英

从Boost库C循环创建图形

[英]Creating graph in loop from Boost library c++

我正在尝试将自己从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”不是。 该矩阵来自称为“基础”的矩阵

   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类的对象。 有人可以帮助我吗? 我很确定这是我正在努力解决的命名约定问题。 如果解决了这个问题,那么我可以通过创建边来解决其余的问题,而我也有尝试使用字符串调用“顶点”类的对象的同样问题。

预先感谢西里尔

表达式!Gra[i][j] == "NA"不会满足您的期望。 它将首先检查Gra[i][j]是否不是“ false”,然后将布尔结果与字符串"NA"

取而代之的是在等式检查周围使用括号,或者进行不等式检查。 所以要么

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

要么

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

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

不用在内部循环内声明新变量,而在循环外声明数组Vertexes

Vertex Vertexes[4][5];

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM