简体   繁体   English

尝试显示我的Graph类,但STL映射迭代器有问题

[英]Trying to display my Graph class, having issues with STL map iterators

My graph class basically consists of a map between values and Vertices , where each Vertex is a class of itself. 我的图类基本上由值和Vertices之间的map组成,其中每个Vertex都是其自身的类。 Each Vertex has both a value and an adjacency list, which is implemented as a map between adjacent Vertices and edge weights. 每个顶点都有一个值和一个邻接表,将其实现为相邻Vertices和边缘权重之间的map I'm trying to display each vertex in the graph along with the vertices it is adjacent to and the weight of the edge that connects it to its adjacent vertices. 我试图显示图形中的每个顶点以及与之相邻的顶点,以及将其连接到其相邻顶点的边缘的权重。 (sorry for all the code) (对不起所有代码)

template <class VertexType>
void Graph<VertexType>::display() const
{
    typedef map<VertexType, Vertex<VertexType> >::iterator vertices_iter;
    typedef map<Vertex<VertexType>, int>::iterator adjList_iter;

    for ( vertices_iter v_iter = vertices.begin(); v_iter != vertices.end(); v_iter++ )
    {
        cout << "Vertex: " << v_iter->second.value << endl; 
        cout << setw(25) << left << "Adjacent to: " << "Edge weight:\n";
        for ( adjList_iter a_iter = vertices[v_iter->first].adjList.begin(); a_iter != vertices[v_iter->first].adjList.end(); a_iter++ )
            cout <<  "    " << a_iter->first.value << "                         " << a_iter->second << endl;
        cout << endl;
    }
}

But I get the following errors: 但是我收到以下错误:

Error 1: 错误1:

error C2440: 'initializing' : cannot convert from 'std::_Tree_const_iterator<_Mytree>' to 'std::_Tree_iterator<_Mytree>'
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tmap_traits<unsigned int,Vertex<unsigned int>,std::less<unsigned int>,std::allocator<std::pair<const unsigned int,Vertex<unsigned int>>>,false>>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>          c:\users\user\desktop\cs m20a\topic g project 2\topic g project 2\graph.h(241) : while compiling class template member function 'void Graph<VertexType>::display(void) const'
1>          with
1>          [
1>              VertexType=unsigned int
1>          ]

Error 2: 错误2:

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)
1>          with
1>          [
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(164): could be 'Vertex<VertexType> &std::map<_Kty,_Ty>::operator [](unsigned int &&)'
1>          with
1>          [
1>              VertexType=unsigned int,
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\map(209): or       'Vertex<VertexType> &std::map<_Kty,_Ty>::operator [](const unsigned int &)'
1>          with
1>          [
1>              VertexType=unsigned int,
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]
1>          while trying to match the argument list '(const std::map<_Kty,_Ty>, const unsigned int)'
1>          with
1>          [
1>              _Kty=unsigned int,
1>              _Ty=Vertex<unsigned int>
1>          ]

and here is my Graph class, if it helps. 这是我的Graph类,如果有帮助的话。

template <class VertexType>
class Graph
{
private:
     // list of all vertices in the graph. assumes non-duplicate data.
    map< VertexType, Vertex<VertexType> > vertices;

    const unsigned MAX_VERTICES;  // Maximum number of vertices the graph can hold.
    unsigned numVertices;         /** Current number of vertices in the graph. */
    unsigned numEdges;            /** Number of edges in the graph. */

    typename map<VertexType, int>::iterator findEdge( const VertexType& v, const VertexType& w ) const;  

public:
   Graph( unsigned max );

   unsigned getNumVertices() const;
   unsigned getMaxNumVertices() const;
   unsigned getNumEdges() const;
   int getWeight( const VertexType& v, const VertexType& w ) const;

   Graph<VertexType>& addVertex( const VertexType& newValue );
   Graph<VertexType>& addEdge( const VertexType& v, const VertexType& w, int weight );
   void removeEdge( const VertexType& v, const VertexType& w );
   void BFS( const VertexType& v ) const;
   void display() const;
}; // end Graph

You're declaring the method as const , that is, it won't change any of the member variables: 您将方法声明为const ,也就是说,它不会更改任何成员变量:

void Graph<VertexType>::display() const

This means you must utilize only const methods of the underlying member variables; 这意味着您只能使用基础成员变量的const方法; hence const_iterator s for your underlying types: 因此const_iterator为您的基础类型:

typedef map<VertexType, Vertex<VertexType> >::const_iterator vertices_iter;
typedef map<Vertex<VertexType>, int>::const_iterator adjList_iter;

Better yet, if you're using C++11 , utilize auto . 更好的是,如果您使用的是C++11 ,请使用auto

As for you second error, I can't see exactly what is causing it, however, you are doing lookup work for no reason: 至于第二个错误,我无法确切知道是什么原因引起的,但是,您无缘无故地进行查找工作:

for(auto a_iter = vertices[v_iter->first].adjList.begin(); 
    a_iter != vertices[v_iter->first].adjList.end(); 
    a_iter++ )

Say v_iter is vertices.begin() in the original loop. 假设v_iter是原始循环中的vertices.begin() Then vertices[v_iter->first] is effectively the same thing as *v.begin() . 然后, vertices[v_iter->first]实际上与*v.begin() You can replace this with: 您可以将其替换为:

for(auto a_iter = v_iter->first.adjList.begin(); 
    a_iter != v_iter->first.adjList.end(); 
    a_iter++ )

The problem is that 问题是

void Graph<VertexType>::display() const

is a const method, so every member of Graph is also const in it. 是const方法,因此Graph的每个成员也都包含const。

Here you use a non-const iterator 在这里,您使用一个非常量迭代器

for ( vertices_iter v_iter = vertices.begin(); v_iter != vertices.end(); v_iter++ )

Try const iterator instead 尝试使用const迭代器

typedef map<VertexType, Vertex<VertexType> >::const_iterator vertices_iter;

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

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