【问题标题】:Issue finding values in a map<char, int>问题在 map<char, int> 中查找值
【发布时间】:2013-05-08 21:17:05
【问题描述】:

当我尝试删除 char 顶点图中的一条边时,它总是会抛出在图中找不到一个或多个顶点的异常。该代码适用于无符号图。

我主要有:

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

removeEdge 函数:

template <class VertexType>
void Graph<VertexType>::removeEdge( const VertexType& v, const VertexType& w )
{
    // 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 removed." );

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

    // remove w from v's adjList and v from w's adjList
    vertices[v].adjList.erase(w);
    vertices[w].adjList.erase(v);

   numEdges--;
}  // end remove

添加顶点:

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

顶点:

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

添加边缘:

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

【问题讨论】:

  • 也许您应该使用无符号整数而不是字符。在散列字符时,我从未看过std::map 的来源,但可能会发生一些愚蠢的事情。
  • @RandyGaul: std::map 不会散列密钥。
  • @dalle Title 说 map,也许应该澄清一下?
  • 你能展示你的addVertex方法和vertices类型吗?
  • 这些看起来不错。我还想看看vertices字段和addEdge方法的定义。

标签: c++ map


【解决方案1】:

uGraph.removeEdge( 'a', 'b' ); -> cGraph.removeEdge( 'a', 'b' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多