如何为一组对象重载操作符

时间:2016-10-22 15:24:00

标签: c++ set overloading

我需要知道如何重载运算符= !=<,以便我可以使用set<set<object> >

我有我的班级:

class pr{
private:
    set<set<e> > pr_;
public:
    pr();
    ~pr();
    void set_pr(set<e> a);
    pr& operator=(const pr& a) const;
    bool operator!=(const pr& a) const;
    bool operator<(const pr& a) const;
};

所以如果我有这样的集合:{{1,2,3},{4,5}}其中数字是对象。

我想对其他类中的集合进行操作,如下所示:

void otherclass::myfunction(){

    pr prt; //I create the objects
    pr prt_old;

    set<e> C1; //I create the subset and fill
    set<e> C2;
     //C1 and C2 filled here    

    prt.set_pr(C1); //Insert the set<e> into set<set<e>>
    prt.set_pr(C2); //It will fail because i dont know how to compare set<e> < <set<e>

    while(prt != prt_old){
        prt_old = prt ;
        prt = create(prt_old);
    }
    //...

我试图超载这样做:

 pr& pr::operator=(const pr& a) const{
   this->clear();

   for(set<set<e> >::iterator it =a.begin();it!=a.end();it++){
       for(set<e>::iterator j = it->begin(); j != it->end();j++){
           this->set_pr(*j);
       }
   }

   return *this;
}

bool pr::operator!=(const pr& a) const{
    if(this->size() != a.size()){
        return 1;
    } 

    //Now i don't know how to continue for check if for example
    // i had {{1,2},{3,4}} and {{1},{2}}
    //this two set have same size but they are differnt
    //How could i just iterate through the two sets at same time
    // and check if subset have too same size or if the objects inside the subset are equal

    //Also i need the operator < to insert a set<e> into a set<set<e> > but how??
//Note: class 'e' has already defined the "operator<" for when I insert objects in the set<e>
//And i order them by a function that return an integrer

1 个答案:

答案 0 :(得分:1)

要测试另一个中是否包含一个集合,请迭代第一个集合中的每个成员,并测试它是否存在于第二个集合中。

    bool operator<(const pr& a) const {
        for (auto _set : _data) {
            if (a._data.find(_set) == a._data.end())
                return false;
        }
        return true;
    }

要测试两组是否相同,您测试它们的大小是否相等,而另一组是否包含在另一组中

    bool operator==(const pr& a) const {
        return _data.size() == a._data.size() && *this < a;
    }

但请注意,无需定义operator==,因为std::set定义的默认值很好。

这是一个功能齐全的计划:

#include <iostream>
#include <set>
using namespace std;

template <class e>
class pr {
private:
    set<set<e> > _data;
public:
    void insert(set<e> a) { _data.insert(a); }
    bool operator==(const pr& a) const {
        return _data.size() == a._data.size() && *this < a;
    }
    bool operator!=(const pr& a) const { return !(*this == a); }
    bool operator<(const pr& a) const {
        for (auto _set : _data) {
            if (a._data.find(_set) == a._data.end())
                return false;
        }
        return true;
    }
};

int main()
{
    pr<int> a,b,c;

    a.insert(set<int>({ 1 }));
    b.insert(set<int>({ 1 }));
    b.insert(set<int>({ 1, 2 }));
    c.insert(set<int>({ 1, 2 }));
    c.insert(set<int>({ 1 }));

    std::cout << ((a<b) ? "a<b\n" : "NOT a<b\n");
    std::cout << ((b<a) ? "b<a\n" : "NOT b<a\n");
    std::cout << ((a==c) ? "a==c\n" : "NOT a==c\n");
    std::cout << ((b==c) ? "b==c\n" : "NOT b==c\n");
    std::cout << ((a==b) ? "a==b\n" : "NOT a==b\n");
    return 0;
}