Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图显示我的Graph类,STL映射迭代器出现问题_C++_Map_Stl_Iterator - Fatal编程技术网

C++ 试图显示我的Graph类,STL映射迭代器出现问题

C++ 试图显示我的Graph类,STL映射迭代器出现问题,c++,map,stl,iterator,C++,Map,Stl,Iterator,我的graph类基本上由值和顶点之间的映射组成,其中每个顶点都是自己的一个类。每个顶点都有一个值和一个邻接列表,邻接列表被实现为相邻顶点和边权重之间的映射。我试图显示图中的每个顶点以及它相邻的顶点,以及连接到相邻顶点的边的权重。(对所有代码表示抱歉) 您将该方法声明为const,也就是说,它不会更改任何成员变量: void Graph<VertexType>::display() const 更好的是,如果您使用的是C++11,请使用auto 至于您的第二个错误,我看不出到底是什么

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


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

void Graph<VertexType>::display() const
更好的是,如果您使用的是
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
顶点。begin()
。那么
顶点[v_iter->first]
实际上与
*v.begin()
是一样的。您可以将其替换为:

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

void Graph<VertexType>::display() const
试试常量迭代器

typedef map<VertexType, Vertex<VertexType> >::const_iterator vertices_iter;
typedef映射::常量迭代器顶点;

谢谢你的回答。这修复了第一个错误,但没有修复第二个错误。@JamesGold我不太确定是什么原因造成的,但您正在执行一个不必执行的额外查找。请参阅我的编辑。
typedef map<VertexType, Vertex<VertexType> >::const_iterator vertices_iter;
typedef map<Vertex<VertexType>, int>::const_iterator adjList_iter;
for(auto a_iter = vertices[v_iter->first].adjList.begin(); 
    a_iter != vertices[v_iter->first].adjList.end(); 
    a_iter++ )
for(auto a_iter = v_iter->first.adjList.begin(); 
    a_iter != v_iter->first.adjList.end(); 
    a_iter++ )
void Graph<VertexType>::display() const
for ( vertices_iter v_iter = vertices.begin(); v_iter != vertices.end(); v_iter++ )
typedef map<VertexType, Vertex<VertexType> >::const_iterator vertices_iter;