C++ C++;具有类键和类值的STL映射容器

C++ C++;具有类键和类值的STL映射容器,c++,map,containers,C++,Map,Containers,假设我有一个这样的类: class Point { private: int x, y; public: void setX(int arg_x) { x = arg_x; } void sety(int arg_y) { y = arg_y; } int getX() const { return x; } int gety() const { return y; } }; map<Point, Point>

假设我有一个这样的类:

class Point
{
   private:
      int x, y;
   public:
      void setX(int arg_x) { x = arg_x; }
      void sety(int arg_y) { y = arg_y; }
      int getX() const { return x; }
      int gety() const { return y; }
};
map<Point, Point> m;
现在我想要一张这样的地图:

class Point
{
   private:
      int x, y;
   public:
      void setX(int arg_x) { x = arg_x; }
      void sety(int arg_y) { y = arg_y; }
      int getX() const { return x; }
      int gety() const { return y; }
};
map<Point, Point> m;
map-m;

但我需要第三个参数。我在cplusplus中读到第三个参数是用来比较某个东西的,但我不明白那个东西是什么。有人能给我解释一下吗?

当您使用用户定义的类作为中的键时,为了确定容器中元素的位置,映射需要比较类:一个接受两个键类型参数并返回bool的类


它基本上是比较两个键值的比较函子/函数。

如果不需要单独的比较函数,可以使用这种方法扩展类

class Point
{
   private:
      int x, y;
   public:

      bool operator<( const Point& other) const
      {
          if ( x == other.x )
          {
              return y < other.y;
          }

          return x < other.x;
      }
};
类点
{
私人:
int x,y;
公众:

bool操作符您不需要第三个参数,只需要
操作符==
操作符您需要的是定义点项目的顺序

这可以通过不同的方式实现:


重载
操作符我认为上面的代码对@parapura rajkumar的解决方案进行了一些升级

class Point{

    private:
        int x, y;

    public:
        bool operator<( const Point& other) const{
            return ((x < other.x) || (y < other.y));
        }
};
类点{
私人:
int x,y;
公众:

bool运算符您所说的第三个参数在STL中称为“Comparator”。 对于作为键的默认类型,您不需要提供一个作为编译器 那工作是为你做的。 但对于您定义的类型,您必须提供它,否则编译器将如何维护它
map/set等中的排序顺序。

第三个参数在哪里?为什么?是否要在map中存储2个点和其他内容?如果要保留set/get函数,请使用公共成员:
struct Point{int x,y;};
更好。映射不需要==运算符。订购的容器不需要
运算符==
。谢谢!这也很有用!谢谢!这非常有用。谢谢!这非常有用。如果x>other.x,这似乎不起作用,因为在这种情况下,它将返回比较y值的结果。@us指出er2963164的编辑被其他人拒绝,尽管它看起来是有效的。@mc110,此答案基于原始帖子。我将在稍后编辑它,以便针对awnser的编辑版本进行更正。
bool operator < (const Point & p_lhs, const Point & p_rhs)
{
    if(p_lhs.getX() < p_rhs.getX()) { return true ; }
    if(p_lhs.getX() > p_rhs.getX()) { return false ; }
    return (p_lhs.getY() < p_rhs.getY()) ;
}
template < class Key, class T, class Compare = less<Key>,
       class Allocator = allocator<pair<const Key,T> > > class map;
struct MyCompareFunctor
{
    bool operator() (const Point & p_lhs, const Point & p_rhs)
    {
       // the code for comparison
    }
} ;
struct MyCompare
{
    bool operator() (const Point & p_lhs, const Point & p_rhs)
    {
        if(p_lhs.getX() > p_rhs.getX()) { return true ; }
        if(p_lhs.getX() < p_rhs.getX()) { return false ; }
        return (p_lhs.getY() > p_rhs.getY()) ;
    }
} ;
std::map<Point, Point, MyCompare> map ;
#include <functional>

namespace std
{
    template<>
    struct less<Point> : binary_function <Point,Point,bool>
    {
        bool operator() (const Point & p_lhs, const Point & p_rhs)
        {
            if(p_lhs.getX() < p_rhs.getX()) { return true ; }
            if(p_lhs.getX() > p_rhs.getX()) { return false ; }
            return (p_lhs.getY() < p_rhs.getY()) ;
        }
    } ;
}
class Point{

    private:
        int x, y;

    public:
        bool operator<( const Point& other) const{
            return ((x < other.x) || (y < other.y));
        }
};