帮助c ++ stl映射和运算符重载

时间:2011-04-01 09:25:00

标签: c++ operator-overloading

我正在尝试将stl地图与我创建的类一起使用,如果我想将我的类Vertex的对象作为地图的关键,我应该重载< ;运算符,我试图在Vertex类中执行此操作,文件graph.h可以在下面看到:

#ifndef GRAPH_H
#define GRAPH_H

class Vertex
{
  private:
    char vertex_name;
  public:
    Vertex(){};

    Vertex(char n)
    {
      vertex_name = n;
    }
//Method signatures
    char get_name(); 
//overloaded operators
    bool operator ==(Vertex other)
    {
      if(this.vertex_name == other.get_name())
      {
        return true;
      }
      else return false;
    }

    bool operator < (Vertex other)
    {
      if(this.vertex_name - other.get_name() < 0)
      {
        return true;
      }
      else return false;
    }
};

class Edge
{
  private:
    Vertex source,destination;
    int weight;
  public:
    Edge(){};
    Edge(Vertex v1,Vertex v2,int w)
    {
      source = v1;
      destination = v2;
      weight = w;
    }

    //Method signatures
    Vertex get_source();
    Vertex get_destn();
    int get_weight();
};

class Graph
{
  private:
    list<Vertex> V;
    list<Edge> E;
    map<Vertex,int> distances;
  public:
    Graph(list<Vertex> vertex_list,list<Edge> edge_list)
    {
      V = vertex_list;
      E = edge_list;
    }

//     Method Signatures
     bool add_vertex(Vertex);
     bool remove_vertex(Vertex);
     bool add_edge(Edge);
     bool remove_edge(Edge);
     int total_vertices();
     int total_edges();
     void initialize_distances(Vertex);
};


/*
 * Methods for the Vertex class are defined first
 */

char Vertex::get_name()
{
  return vertex_name;
}


/*
 * Methods for the Edge class are defined next
 */

int Edge::get_weight()
{
  return weight;
}

Vertex Edge::get_destn()
{
  return destination;
}

Vertex Edge::get_source()
{
  return source;
}

/*
 * Methods for our Graph class
 */

bool Graph::add_vertex(Vertex u)
{
  V.push_back(u);
}

bool Graph::add_edge(Edge e)
{
  E.push_back(e);
}

//slightly more tricky will write code when it'll be required i.e. when i implement dfs or some other algo
//that requires addition and removal of edges and vertices
bool Graph::remove_vertex(Vertex u)
{
  //first check if it exists
  //when a vertex is removed then then all the edges that have it as either a source or a destination should also be removed
}

//
bool Graph::remove_edge(Edge e)
{
  //much easier than removing a vertex
  //check if the edge exists and if it does remove it from the list..
}

int Graph::total_edges()
{
  return E.size();
}

int Graph::total_vertices()
{
  return V.size();
}

void Graph::initialize_distances(Vertex source)
{
  distances.clear();
  for(list<Vertex>::iterator it=V.begin(); it != V.end();it++)
  {
    //todo : overload = for the class vertex
    if( *it == source)
    {
      distances[*it] = 0;
    }
    else
    {
      distances[*it] = INT_MAX;
    }
  }
}



#endif //GRAPH_H

包含此头文件bellman_ford.cpp的c ++文件如下:

#include<iostream>
#include<list>
#include<map>
#include<climits>

using namespace std;

#include "graph.h"

int main()
{
  Graph G = Graph(list<Vertex>(), list<Edge>());
  int vertices;
  cout<<"Enter the no. of vertices : ";
  cin>>vertices;

  for(int i=0;i<vertices;i++)
  {
    cout<<"Enter the name of the vertex( one character only ) : ";
    char tmp;
    cin>>tmp;
    Vertex tmp_vertex =  Vertex(tmp);
    G.add_vertex(tmp_vertex);
  }

  char choice;
  do
  {
    char tmp_src,tmp_destn;
    int tmp_w;
    cout<<"Enter edge( source, destn, weight)";
    cin>>tmp_src>>tmp_destn>>tmp_w;
    G.add_edge( Edge(Vertex(tmp_src),Vertex(tmp_destn),tmp_w) );

    cout<<"Add another edge (y|n)? ";
    cin>>choice;
  }while( choice != 'n');



  return 0;
}

我认为我正在做超载错误,任何关于如何去做的指针都将非常感激。

3 个答案:

答案 0 :(得分:5)

operator<的原型应为:

bool operator<(const Vertex& b) const { ... }

operator==的内容应该是const,并且需要const参考。 Vertex::get_name()也应为const

答案 1 :(得分:3)

我认为你可能需要让你的操作符为const

bool operator< ( Vertex other ) const

如果数据很大,你也可以考虑通过const引用传递另一个Vertex(在这种情况下它似乎不是

bool operator< ( const Vertex & other ) const

答案 2 :(得分:0)

const 运算符对其他实例的 const 引用的建议是好建议,但问题的本质是什么?

请指定您是否遇到编译时错误,运行时错误,或只是遇到意外行为(地图未排序如您所愿。)