2016-09-19 65 views
0

我試圖讓枚舉圖中所有節點值的假迭代器。只要序列由圖中所有且僅有的節點組成,該圖就可以以任何順序抽象。應該有一個可變的限定符,並且這4個操作的實現很短,應該是每個函數只有1行。枚舉圖中所有節點值的假迭代器操作

測試案例爲4個假迭代器操作情況如下,假設通過假迭代操作,打印出的圖節點的值的字符串:

gdwg::Graph<std::string,int>> g 
    for (g.begin(); !g.end(); g.next()) 
     std::cout << g.value() << std::end; 

我宣佈圖形作爲以下:

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(); 
    }; 

    // fake iterator operations 
    void begin() const; 
    bool end() const; 
    void next() const; 
    const N& value() const; 



public: 

    friend class Node_Iterator<N, E>; 
    friend class Edge_Iterator<N, E>; 


private: 
    std::map< N, std::shared_ptr<Node> > nodes_; 

    // fake iterator to be used for the four operations begin(), end(), next() and value() 
    mutable typename std::map< N, std::shared_ptr<Node> >::iterator fakeIter_; 

}; 

4個操作的執行如下:

// void begin() const: Sets an internal iterator, i.e., ``pointer'' to the first element of a sequence. 
template <typename N, typename E> 
void Graph<N,E>::begin() const { 
    // gets iterator to the first key/value pair in map sequence 
    fakeIter_ = nodes_.begin(); 
} 

// bool end() const: Returns true if the iterator goes past the last element of the sequence and false otherwise. 
template <typename N, typename E> 
bool Graph<N,E>::end() const { 
    // return true if iterator goes past last element, otherwise return false 
    return ((fakeIter_ == nodes_.end()) ? true : false); 
} 

// void next() const: Moves the iterator to the next element of the sequence. 
template <typename N, typename E> 
void Graph<N,E>::next() const { 
    fakeIter_ = std::next(fakeIter_, 1); 
} 

// const N& value() const: Returns the value of the node pointed to by the iterator. 
template <typename N, typename E> 
const N& Graph<N,E>::value() const { 
    return fakeIter_->second->val_; 
} 

當我嘗試編譯時,彈出一堆錯誤。我只是想知道我是否正確實現了假的迭代器操作,並且如果有什麼方法來改進它,如果需要的話?

tests/Graph.tem: In instantiation of ‘void gdwg::Graph<N, E>::begin() const [with N = unsigned int; E = int]’: 
tests/test13.cpp:23:15: required from here 
tests/Graph.tem:713:12: error: no match for ‘operator=’ (operand types are ‘std::map<unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node>, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > > >::iterator {aka std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >}’ and ‘std::map<unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node>, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > > >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >}’) 
    fakeIter_ = nodes_.begin(); 

In file included from /usr/local/include/c++/6.1.0/map:60:0, 
       from tests/Graph.h:19, 
       from tests/test13.cpp:3: 
/usr/local/include/c++/6.1.0/bits/stl_tree.h:174:12: note: candidate: constexpr std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >& std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >::operator=(const std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >&) 
    struct _Rb_tree_iterator 
      ^~~~~~~~~~~~~~~~~ 
/usr/local/include/c++/6.1.0/bits/stl_tree.h:174:12: note: no known conversion for argument 1 from ‘std::map<unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node>, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > > >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >}’ to ‘const std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >&’ 
/usr/local/include/c++/6.1.0/bits/stl_tree.h:174:12: note: candidate: constexpr std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >& std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >::operator=(std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >&&) 
/usr/local/include/c++/6.1.0/bits/stl_tree.h:174:12: note: no known conversion for argument 1 from ‘std::map<unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node>, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > > >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >}’ to ‘std::_Rb_tree_iterator<std::pair<const unsigned int, std::shared_ptr<gdwg::Graph<unsigned int, int>::Node> > >&&’ 

回答

1

什麼是第一個錯誤是說的是,作爲你的begin功能constnodes_.begin()將返回const_iterator。這不能被分配到iterator,類型fakeIter_

你需要的constbegin刪除,更改fakeIter_類型,或者改變您的實現(begin應該返回一個迭代器,而不是無效)。

+0

我無法從開始刪除const,或根據規範將實現的返回類型從void更改爲迭代器。所以我唯一能做的就是根據你所說的改變fakeIter_的類型。我該如何改變它? – iteong

+0

哦,我知道了。只需將:: iterator更改爲:: const_iterator即可。 – iteong