C++ 用于插入集合的成对相等运算符重载

C++ 用于插入集合的成对相等运算符重载,c++,operator-overloading,set,std-pair,C++,Operator Overloading,Set,Std Pair,我正在尝试将一对添加到集合中。如果一对与集合中的另一对共享相同的两个值,则不应插入该对 以下是我的非工作代码: typedef std::pair<int, int> PairInt; template<> bool std::operator==(const PairInt& l, const PairInt& r) { return (l.first == r.first && l.second == r.second)

我正在尝试将一对
添加到集合中。如果一对与集合中的另一对共享相同的两个值,则不应插入该对

以下是我的非工作代码:

typedef  std::pair<int, int> PairInt;

template<>
bool std::operator==(const PairInt& l, const PairInt& r) 
{
    return (l.first == r.first && l.second == r.second) ||
           (l.first == r.second && l.second == r.first);
}

int main()
{
    std::set<PairInt> intSet;
    intSet.insert(PairInt(1,3));
    intSet.insert(PairInt(1,4));
    intSet.insert(PairInt(1,4));
    intSet.insert(PairInt(4,1));
}
我希望是这样

(1 3)
(1 4)

我曾尝试在重载方法中放置断点,但从未到达它们。我做错了什么?

您需要提供一个比较函数,以查看一个项目是否小于另一个项目,而不是确定它们是否相等。下面是一个完整的示例:

#include <utility>
#include <algorithm>
#include <set>
#include <iostream>

typedef  std::pair<int, int> PairInt;
typedef bool Compare(const PairInt &,const PairInt &);

bool compare(const PairInt &l,const PairInt &r)
{
  int lfirst = std::min(l.first,l.second);
  int rfirst = std::min(r.first,r.second);
  if (lfirst<rfirst) return true;
  if (rfirst<lfirst) return false;
  return std::max(l.first,l.second)<std::max(r.first,r.second);
}

int main()
{
  typedef std::set<PairInt,Compare*> IntSet;
  IntSet intSet(compare);
  intSet.insert(PairInt(1,3));
  intSet.insert(PairInt(1,4));
  intSet.insert(PairInt(1,4));
  intSet.insert(PairInt(4,1));
  for (IntSet::const_iterator i=intSet.begin(); i!=intSet.end(); ++i) { 
    std::cerr << i->first << "," << i->second << "\n";
  } 
}

集合基于
运算符比较应确定第一项是否小于第二项。所以它应该是这样的:

namspace std
{
  template<>
  bool operator < (const PairInt& l, const PairInt& r) 
  {
     //swap only if they're unequal to avoid infinite recursion
     if (l.first != l.second) 
     {
         //swap elements, considering your special case
          if (l.first == r.second && l.second == r.first)
             return l < PairInt(r.second, r.first); //call again!
     }

    //actual comparison is done here
    if ( l.first != r.first )
       return l.first < r.first;
    else 
       return l.second < r.second;
  }
}
看一看这张照片


请注意,比较功能如下所示:

正在使用自定义设置。。。只是想知道,你上面发布的方法和CPluPlus网站上的方法有什么不同?我发现您的代码更容易理解,但仍然不太清楚,例如,typedef中的
(*)
。)@朱托:我添加了一个typedef,希望能让它更清晰一点。如果仍然不清楚,请告诉我。你能给我一个链接到你所说的cplusplus网站上的方法吗?@Nawaz你能提供类似的重载方法吗<对于一个包含3elements@aksam:先试试你自己。那么让我知道你已经尝试了什么。也许,那么我会帮忙。
1,3
1,4
#include <set>
#include <utility>
#include <cassert>
typedef std::pair<int, int> PairInt;
PairInt normalize(const PairInt& p) {
    return p.second < p.first ? PairInt(p.second, p.first) : p;
}
struct Comparator {
    bool operator()(const PairInt& l, const PairInt& r) const {
        //Compare canonical forms of l and r.
        return normalize(l) < normalize(r);
    }
};

int main()
{
    std::set<PairInt, Comparator> intSet;
    intSet.insert(PairInt(1,3));
    intSet.insert(PairInt(1,4));
    intSet.insert(PairInt(1,4));
    intSet.insert(PairInt(4,1));
    assert(intSet.size() == 2);
}
namspace std
{
  template<>
  bool operator < (const PairInt& l, const PairInt& r) 
  {
     //swap only if they're unequal to avoid infinite recursion
     if (l.first != l.second) 
     {
         //swap elements, considering your special case
          if (l.first == r.second && l.second == r.first)
             return l < PairInt(r.second, r.first); //call again!
     }

    //actual comparison is done here
    if ( l.first != r.first )
       return l.first < r.first;
    else 
       return l.second < r.second;
  }
}
1,3
1,4