gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:18:26 25 4
gpt4 key购买 nike

我的图类基本上由值和Vertices 之间的map 组成,其中每个Vertex 都是其自身的一个类。每个 Vertex 都有一个值和一个邻接列表,它被实现为相邻 Vertices 和边权重之间的 map。我正在尝试显示图中的每个顶点及其相邻的顶点以及将它连接到相邻顶点的边的权重。 (抱歉所有代码)

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

但是我得到以下错误:

错误一:

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> ]

错误 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> ]

这是我的 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

最佳答案

您将方法声明为 const,也就是说,它不会更改任何成员变量:

void Graph<VertexType>::display() const

这意味着您只能使用底层成员变量的const 方法;因此 const_iterator 用于您的基础类型:

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

更好的是,如果您使用的是 C++11,请使用 auto

至于你的第二个错误,我看不出到底是什么导致了它,但是,你无缘无故地在做查找工作:

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

假设 v_iter 是原始循环中的 vertices.begin()。然后 vertices[v_iter->first] 实际上与 *v.begin() 相同。您可以将其替换为:

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

关于c++ - 尝试显示我的 Graph 类,STL 映射迭代器有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16435254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com