2016-09-18 159 views
0

我試圖訪問我的Node結構中的edges_,所以我可以做一個for-loop將邊緣複製到我的拷貝構造函數的新圖對象中。新圖的深拷貝構造函數

我收到以下錯誤,當我嘗試訪問節點中的edges_時,我感到困惑。

tests/Graph.tem:280:24: error: ‘struct std::pair<const std::__cxx11::basic_string<char>, std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node> >’ has no member named ‘edges_’ 
    for (auto edge: node.edges_) { 
        ~~~~~^~~~~~ 

我試圖做一個拷貝構造函數,深拷貝到一個新的圖形對象的圖形中的節點和邊緣:

template <typename N, typename E> 
Graph<N, E>::Graph(const Graph &g): 
    nodes_{g.nodes_} 
    { 

     for (auto node: g.nodes_) { 

      for (auto edge: node.edges_) { 

      } 

     } 


    } 

以下是我的Graph類:

template <typename N, typename E> class Graph { 

    private: 
     struct Node; 
     struct Edge; 

     struct Node { 
      N val_; 
      int numEdges_; 
      int numIncomingEdges_; 
      std::set<std::shared_ptr<Edge>> edges_; 
      std::set<std::shared_ptr<Edge>> incomingEdges_; 
      Node() {} 
      Node(const N x) : val_{x} { numEdges_=0; numIncomingEdges_=0; } 
      void printNode(N n); 
      ~Node(); 
      void update(); 
     }; 

     struct Edge { 
      std::weak_ptr<Node> orig; 
      std::weak_ptr<Node> dest; 
      E val_; 
      Edge(std::shared_ptr<Node> o, std::shared_ptr<Node> d, E x); 
      Edge() {}; 
      void printEdge(); 
      ~Edge(); 
     }; 

首先,如何訪問它以執行深度複製?似乎有一些ptr問題。其次,是否有一種簡單的方法可以深入複製存儲在節點中的邊緣?

+0

什麼是Graph :: nodes_'的類型?如果它是'std :: map >',如你的錯誤消息所示,'node'具有類型'std :: pair > ',所以而不是'node.edges_',你應該使用'node.second-> edges_'。 – Franck

+0

std :: map > nodes_; – iteong

+0

因此,對於第一個問題,您應該用'for(auto edge:node.second-> edges_)'替換'for(auto edge:node.edges_)'。 – Franck

回答

1

對於編譯器的消息,你應該for (auto edge: node.second->edges_)

更換for (auto edge: node.edges_)要進行深拷貝,你需要的g源節點和副本的節點之間的關聯圖。

這是我爲你的深拷貝構造函數編寫的代碼的一個想法(我還沒有試圖編譯它)。

template <typename N, typename E> 
Graph<N, E>::Graph(const Graph &g): 
    nodes_{g.nodes_} 
    { std::map<Node*, Node*> associativeMap; 
     typename std::map<N, std::shared_ptr<Node>>::const_iterator 
      thisIter(nodes_.begin()), sourceIter(g.nodes_.begin()), 
      thisIterEnd(nodes_.end()), sourceIterEnd(g.nodes_.end()); 
     for (; thisIter != thisIterEnd; ++thisIter) { 
      associativeMap.insert(std::make_pair(&*(sourceIter->second), &*(thisIter->second)); 
      ++sourceIter; 
     } 

     thisIter = nodes_.begin(); 
     for (auto sourceNode: g.nodes_) { 
      Node* thisNode = &*thisIter->second; 
      for (auto sourceEdge: sourceNode.second->edges_) 
       addEdge(*thisNode, *associativeMap[&*sourceEdge->dest], ...); 
      ++thisIter; 
     } 
    } 

它是基於一個addEge方法與簽名void addEdge(Node& origin, Node& destination, ...)

如果您的Graph :: nodes_很快被排序,並且addEdge可以從密鑰中檢索節點 - 如果其簽名是bool addEdge(const N& orig, const N& dest, const E& val) - ,則associativeMap不再有用。在這種情況下,代碼要簡單得多。

template <typename N, typename E> 
Graph<N, E>::Graph(const Graph &g): nodes_{g.nodes_} 
{ for (auto sourceNode: g.nodes_) { 
     for (auto sourceEdge: sourceNode.second->edges_) 
     addEdge(sourceNode.second->val_, sourceEdge->dest.lock()->val_, sourceEdge->val_); 
    } 
}