C++ 在地图中查找值的问题<;字符,int>;

C++ 在地图中查找值的问题<;字符,int>;,c++,map,C++,Map,当我试图删除字符顶点图中的边时,它总是抛出一个例外,即在该图中找不到一个或多个顶点。该代码适用于无符号图 我主要有: Graph<char> cGraph; cGraph.addVertex( 'a' ).addVertex( 'b' ); cGraph.addEdge( 'a', 'b', 6 ); cout << "\n\nAttempt to remove an edge with existing vertices:\n\n"; try {

当我试图删除字符顶点图中的边时,它总是抛出一个例外,即在该图中找不到一个或多个顶点。该代码适用于无符号图

我主要有:

Graph<char> cGraph;

cGraph.addVertex( 'a' ).addVertex( 'b' );

cGraph.addEdge( 'a', 'b', 6 );

cout << "\n\nAttempt to remove an edge with existing vertices:\n\n";
    try
    {
        uGraph.removeEdge( 'a', 'b' );
        cout << "Edge < 'a', 'b' > has been removed from the graph.\n";
    }
    catch ( GraphException& e )
    {
        cout << e.what();
    }
图形cGraph;
cGraph.addVertex('a')。addVertex('b');
cGraph.addEdge('a','b',6);
cout 0;
如果(!vIsInGraph | |!wIsInGraph)
抛出GrapherException(“在图中未找到一个或两个顶点。未删除边”);
if(findEdge(v,w)==顶点[v].adjList.end())
抛出grapherException(“两个顶点之间不存在边。边未删除”);
//从v的调整列表中删除w,从w的调整列表中删除v
顶点[v].adjList.erase(w);
顶点[w].adjList.erase(v);
努梅德斯--;
}//结束删除
添加顶点:

template <class VertexType>
Graph<VertexType>& Graph<VertexType>::addVertex( const VertexType& newValue )
{
    Vertex<VertexType> newVertex( newValue );

    if ( numVertices == MAX_VERTICES )
    {
        string MAX_VERTICES_s;
        stringstream stream;
        stream << MAX_VERTICES;
        stream >> MAX_VERTICES_s;
        throw GraphException( "Adding vertex " +  static_cast<string>( newVertex ) + " to the graph would exceed the graph's maximum capacity of " + MAX_VERTICES_s + "."  );
    }

    vertices[newValue] = newVertex;
    numVertices++;

    return *this;
}
模板
Graph&Graph::addVertex(const VertexType&newValue)
{
顶点newVertex(newValue);
if(numVertices==最大顶点)
{
字符串最大顶点数;
溪流;
流>最大顶点数;
抛出GrapherException(“向图中添加顶点”+静态_cast(newVertex)+”将超过图的最大容量“+最大顶点_s+”);
}
顶点[新值]=新顶点;
numVertices++;
归还*这个;
}
顶点:

template <class VertexType>
class Vertex
{
private:
    static unsigned counter;

    // each vertex in the graph has both a value and a list of other vertices it is adjacent to
public:
    static unsigned ID;
    VertexType value;
    // maps an adjacent vertex to the weight of the edge connecting the two vertices
    map< VertexType, int > adjList;

    Vertex() { ID = counter++; }

    bool operator<(const Vertex& v) const;
    Vertex( const VertexType& p_value ) : value( p_value ) {}
    // allows a vertex to be converted to a string (for display purposes)
    operator string() const;
};
模板
类顶点
{
私人:
静态无符号计数器;
//图中的每个顶点都有一个值以及与其相邻的其他顶点的列表
公众:
静态无符号ID;
顶点类型值;
//将相邻顶点映射到连接两个顶点的边的权重
mapadjList;
顶点(){ID=counter++;}
布尔运算符0,
wIsInGraph=顶点。计数(w)>0;
如果(!vIsInGraph | |!wIsInGraph)
抛出GraphException(“在图中未找到一个或两个顶点。未添加边”);
if(findEdge(v,w)!=顶点[v].adjList.end()
抛出GrapherException(“两个顶点之间已经存在一条边。未添加边”);
顶点[v]。调整列表[w]=权重;
顶点[w]。调整列表[v]=权重;
numEdges++;
归还*这个;
}//结束添加
具有顶点声明的图形类:

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

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 unsigned MAX_texts;//图可以容纳的最大顶点数。
无符号numVertices;/**图中当前的顶点数*/
无符号numEdges;/**图形中的边数*/
typename映射:迭代器findEdge(const VertexType&v,const VertexType&w);
公众:
图(无符号最大值);
未签名的getNumVertices()常量;
未签名的getMaxNumVertices()常量;
未签名的getNumEdges()常量;
int getWeight(const VertexType&v,const VertexType&w)const;
图形和添加顶点(const VertexType和newValue);
图形和补遗(常量VertexType&v、常量VertexType&w、整数权重);
void removedge(const VertexType&v、const VertexType&w);
void BFS(const VertexType&v)const;
void display()常量;
}; // 结束图

uGraph.removedge('a','b')->
cGraph.removedge('a','b')

也许您应该使用无符号整数而不是字符。在散列字符时,我从未查看过
std::map
的源代码,但可能会有一些愚蠢的事情发生。@RandyGaul:
std::map
不会散列键。@dalle Title说map,也许应该澄清一下?你能显示你的
addVertex
方法和
顶点
类型吗?这些看起来不错。我还想了解
顶点
字段和
addEdge
方法的定义。
template <class VertexType>
Graph<VertexType>& Graph<VertexType>::addEdge( const VertexType& v, const VertexType& w, int weight )
{
    // error checking
     bool vIsInGraph = vertices.count( v ) > 0,
          wIsInGraph = vertices.count( w ) > 0;

   if ( !vIsInGraph || !wIsInGraph )
      throw GraphException( "One or both vertices not found in graph. Edge not added." );

    if ( findEdge(v, w) != vertices[v].adjList.end() )
        throw GraphException( "An edge already exists between the two vertices. Edge not added." );

    vertices[v].adjList[ w ] = weight;
    vertices[w].adjList[ v ] = weight;

    numEdges++;

    return *this;
}  // end addEdge
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 ) ;  

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