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

发布于 2024-10-29 12:12:22 字数 3855 浏览 1 评论 0原文

我正在尝试将 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;
}

我认为我做的重载错误,任何指针关于如何进行的,我们将不胜感激。

i'm trying to use the stl map with a class that i've created, from what i've gathered if i want an object of my class Vertex to be the key for the map, i should also overload the < operator, i've attempted to do this in the class Vertex the file graph.h can be seen below:

#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

the c++ file that includes this header file bellman_ford.cpp is as below :

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

i think i'm doing the overloading wrong, any pointers on how to go about it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒眉千度 2024-11-05 12:12:22

operator< 的原型应该是:

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

operator== 也是如此,应该是 const,并采用 const参考。
Vertex::get_name() 也应该是 const

The prototype for operator< should be:

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

Same thing for your operator==, should be const, and take a const reference.
The Vertex::get_name() should also be const.

千纸鹤 2024-11-05 12:12:22

我认为你可能需要使你的运算符成为 const

bool operator< ( Vertex other ) const

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

bool operator< ( const Vertex & other ) const

I think you may need to make your operators const

bool operator< ( Vertex other ) const

You might also consider passing the other Vertex by const reference if the data is large, (which it doesn't seem to be in this case

bool operator< ( const Vertex & other ) const
给妤﹃绝世温柔 2024-11-05 12:12:22

关于 const 运算符将 const 引用引用到另一个实例的建议是很好的建议,但是问题的本质是什么?

请指定您是否遇到编译时错误、运行时错误,或者只是遇到意外行为(地图未按照您的方式排序更喜欢)。

The advice about const operators taking const references to the other instance is good advice, however what is the nature of the problem?

Please specify whether you are experiencing a compile time error, a runtime error, or just experiencing unexpected behaviour (the map is not sorted as you would prefer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文