c++ - 如何为一组对象重载运算符

标签 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

最佳答案

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

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

关于c++ - 如何为一组对象重载运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193793/

相关文章:

C++,OpenCV : Assertion failed in Resize

c++ - 虚拟内存耗尽 : Cannot allocate memory with 8 gb ram

R:如何通过仅比较每个字符串中的前 3 个制表符分隔项来对两个字符串向量使用 setdiff?不使用 qdap

dictionary - 如何从 es6 Map 或 Set 中获取随机项目

c++ - 增量后重载中的冗余?

c++ - 将函数注入(inject)子类

c++ - noskipws对cin的影响>>

c++ - 编译 DirectX11 着色器文件

c++ - 在集合中搜索项目的一部分

Java类没有执行正确的实现重载方法