簡體   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