Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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++ 帮助使用c++;stl映射与操作符重载_C++_Operator Overloading - Fatal编程技术网

C++ 帮助使用c++;stl映射与操作符重载

C++ 帮助使用c++;stl映射与操作符重载,c++,operator-overloading,C++,Operator Overloading,我试图将stl映射与我创建的类一起使用,根据我收集的信息,如果我希望类Vertex的对象作为映射的键,我还应该重载

我试图将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
#ifndef图
#定义图
类顶点
{
私人:
字符名称;
公众:
顶点(){};
顶点(字符n)
{
顶点名称=n;
}
//方法签名
char get_name();
//重载运算符
布尔运算符==(顶点其他)
{
if(this.vertex\u name==other.get\u name())
{
返回true;
}
否则返回false;
}
布尔运算符<(顶点其他)
{
if(this.vertex_name-other.get_name()<0)
{
返回true;
}
否则返回false;
}
};
阶级边缘
{
私人:
顶点源、目标;
整数权重;
公众:
边(){};
边(顶点v1,顶点v2,整数w)
{
来源=v1;
目的地=v2;
重量=w;
}
//方法签名
顶点获取_源();
顶点获取_destn();
int get_weight();
};
类图
{
私人:
清单五;
清单E;
地图距离;
公众:
图(列表顶点列表、列表边列表)
{
V=顶点列表;
E=边缘列表;
}
//方法签名
布尔加_顶点(顶点);
bool移除_顶点(顶点);
布尔加_边(边);
bool去除_边(边);
int total_顶点();
int总_边();
无效初始化距离(顶点);
};
/*
*首先定义顶点类的方法
*/
char Vertex::get_name()
{
返回顶点名称;
}
/*
*接下来定义Edge类的方法
*/
int-Edge::get_-weight()
{
返回重量;
}
顶点边::get_destn()
{
返回目的地;
}
顶点边::获取_源()
{
返回源;
}
/*
*图类的方法
*/
布尔图::添加_顶点(顶点u)
{
V.推回(u);
}
布尔图::添加_边(边e)
{
E.推回(E);
}
//在需要的时候,也就是说,当我实现dfs或其他算法时,编写代码要稍微复杂一些
//这需要添加和删除边和顶点
布尔图::删除顶点(顶点u)
{
//首先检查它是否存在
//删除顶点后,将其作为源或目标的所有边也应删除
}
//
布尔图::删除_边(边e)
{
//比删除顶点容易得多
//检查该边是否存在,以及是否将其从列表中删除。。
}
int图::总边数()
{
返回E.size();
}
int图::总顶点数()
{
返回V.size();
}
空图::初始化距离(顶点源)
{
距离。清除();
for(list::iterator it=V.begin();it!=V.end();it++)
{
//todo:重载=对于类顶点
如果(*it==源)
{
距离[*it]=0;
}
其他的
{
距离[*it]=INT_MAX;
}
}
}
#endif//GRAPH\u H

包含该头文件Belman的文件的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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
#包括“graph.h”
int main()
{
图G=图(list(),list());
int顶点;
能力;
对于(inti=0;i>tmp\u destn>>tmp\u w;
G.添加边(边(顶点(tmp_src)、顶点(tmp_destn)、tmp_w));
coutchoice;
}while(choice!=“n”);
返回0;
}

我认为我做的重载是错误的,任何关于如何进行重载的建议都将不胜感激。

我认为您可能需要将运算符设置为常量

bool operator< ( Vertex other ) const
bool运算符<(顶点其他)常量

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

)。
bool operator< ( const Vertex & other ) const
bool运算符<(常数顶点和其他)常数

关于const操作符引用另一个实例的
操作符的原型关于const操作符的建议是很好的建议,但是问题的本质是什么


请指定您是遇到编译时错误、运行时错误,还是只是遇到意外行为(映射未按您希望的方式排序)。

实际问题是什么?应用程序是否编译?不确定这是否会改变任何内容,但请尝试使用常量引用参数定义比较器重载(即Vertex const&other)。我无法更早地编译它,编译错误非常可怕且难以理解。感谢所有帮助过我的人,它现在确实可以工作了。
bool operator<(const Vertex& b) const { ... }