如何定义运算符<对于结构?

时间:2018-04-09 20:27:51

标签: c++ struct operators

我有结果:

struct Arco {

    int i, j;
    Arco () {};
    Arco (const Arco& obj): i(obj.i), j(obj.j) {};
    Arco(int _i, int _j) : i(_i), j(_j) {}    

};

struct ARCO_TEMPO {
    Arco a;
    int slotTimeU; 
    int slotTimeV; 
    ARCO_TEMPO () {};
    ARCO_TEMPO (const ARCO_TEMPO& obj): a(obj.a), slotTimeU(obj.slotTimeU), slotTimeV(obj.slotTimeV) {};
    ARCO_TEMPO (Arco _a, int _slotTimeU, int _slotTimeV) : a(_a), slotTimeU(_slotTimeU), slotTimeV(_slotTimeV) {}    

};

struct CICLO {
    set<ARCO_TEMPO> arco_tempo_order;
    set<int> arco_tempo_Aux;
    int aircraftType; 
    float COST;
    float reducedCost;
    float duracao;
    int numAircrafts;
    vector<bool> VisitedNodes; 
    vector<vector<bool>> VisitedVertices;
};

我需要定义运算符&lt;到结构CICLO。由于我想通过他们的COST订购CICLO,我以他们的COST开始比较。接下来,我验证CICLO是否由同一架飞机操作,然后验证它们是否具有相同数量的ARCOs_TEMPO。 如果是这种情况,我需要验证这些ARCOs_TEMPO是否相同。 使用这些想法,我编写了以下代码: (我写了同样的couts试图分析问题的位置)

bool operator<(const CICLO& obj1, const CICLO& obj2) {

    cout << "obj1  = { aircraft type: "<< obj1.aircraftType << " " ;
    set<ARCO_TEMPO>::iterator itobj1;
    for (itobj1 = obj1.arco_tempo_order.begin();  itobj1 != obj1.arco_tempo_order.end(); itobj1++) {
        cout << "(" << itobj1->a.i+1 << "," << itobj1->a.j+1 << ")-("<<itobj1->slotTimeU<<", "<<itobj1->slotTimeV << "); ";
    }
    printf (" COST: %.0f }\n", obj1.COST);

    cout << "obj2  = { aircraft type: "<< obj2.aircraftType << " " ;
    set<ARCO_TEMPO>::iterator itobj2;
    for (itobj2 = obj2.arco_tempo_order.begin();  itobj2 != obj2.arco_tempo_order.end(); itobj2++) {
        cout << "(" << itobj2->a.i+1 << "," << itobj2->a.j+1 << ")-("<<itobj2->slotTimeU<<", "<<itobj2->slotTimeV << "); ";
    }
    printf (" COST: %.0f }\n", obj2.COST);

    if (obj1.COST < obj2.COST - 1) {
        cout << "1\n";
        return true;
    }
    else {
        if ( (abs(obj1.COST - obj2.COST) < 1) && obj1.aircraftType < obj2.aircraftType) {
            cout << "2\n";
            return true;
        }
        else {
            if (obj1.aircraftType == obj2.aircraftType && (abs(obj1.COST - obj2.COST) < 1) && obj1.arco_tempo_order.size() < obj2.arco_tempo_order.size()) {
                cout << "3\n";
                return true;
            }
            else {
                if (obj1.aircraftType == obj2.aircraftType && (abs(obj1.COST - obj2.COST) < 1) && obj1.arco_tempo_order.size() == obj2.arco_tempo_order.size()) {
                    bool igual = true;
                    set<ARCO_TEMPO>::iterator itobj1;
                    set<ARCO_TEMPO>::iterator itobj2;
                    for (itobj1 = obj1.arco_tempo_order.begin(), itobj2 = obj2.arco_tempo_order.begin();  itobj1 != obj1.arco_tempo_order.end(); itobj1++,itobj2++) {
                        if (igual && *itobj1 < *itobj2) {
                            cout << "4\n";
                            return true;
                        } 
                        else {
                            if (itobj1->a.i == itobj2->a.i && itobj1->a.j == itobj2->a.j && itobj1->slotTimeU == itobj2->slotTimeU && itobj1->slotTimeV == itobj2->slotTimeV) {
                                igual = true;
                            }
                            else {
                                cout << "5\n";
                                return false;
                            }
                        }
                    }
                    cout << "6\n";
                    return false;
                }
                else{ 
                    cout << "7\n"; 
                    return false;
                }
            }
        }
    }
}

但是,不同的周期被认为是平等的。 我有一套CICLO:

set<CICLO>ConjCiclos;

我在ConjCiclos中插入了一些inicial CICLOS,其中ConjCiclos =

CICLO[1] = { aircraft type: 2; arco_tempo_order: (4,5)-(1, 2); (5,4)-(2, 3); (4,4)-(3, 4); (4,4)-(4, 5); (4,4)-(5, 6); (4,4)-(6, 7); (4,4)-(7, 1);  COST: 25000096 }
CICLO[2] = { aircraft type: 2; arco_tempo_order:  (1,5)-(1, 2); (5,1)-(2, 3); (1,1)-(3, 4); (1,1)-(4, 5); (1,1)-(5, 6); (1,1)-(6, 7); (1,1)-(7, 1);  COST: 25000142 }
CICLO[3] = { aircraft type: 2; arco_tempo_order:  (2,5)-(1, 2); (5,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000164 }
CICLO[4] = { aircraft type: 2; arco_tempo_order:  (1,2)-(1, 2); (2,1)-(2, 3); (1,1)-(3, 4); (1,1)-(4, 5); (1,1)-(5, 6); (1,1)-(6, 7); (1,1)-(7, 1);  COST: 25000220 }
CICLO[5] = { aircraft type: 2; arco_tempo_order:  (1,4)-(1, 2); (4,1)-(2, 3); (1,1)-(3, 4); (1,1)-(4, 5); (1,1)-(5, 6); (1,1)-(6, 7); (1,1)-(7, 1);  COST: 25000228 }
CICLO[6] = { aircraft type: 2; arco_tempo_order:  (2,4)-(1, 2); (4,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000232 }
CICLO[7] = { aircraft type: 2; arco_tempo_order:  (3,5)-(1, 2); (5,3)-(2, 3); (3,3)-(3, 4); (3,3)-(4, 5); (3,3)-(5, 6); (3,3)-(6, 7); (3,3)-(7, 1);  COST: 25000284 }
CICLO[8] = { aircraft type: 2; arco_tempo_order:  (3,4)-(1, 2); (4,3)-(2, 3); (3,3)-(3, 4); (3,3)-(4, 5); (3,3)-(5, 6); (3,3)-(6, 7); (3,3)-(7, 1);  COST: 25000326 }
CICLO[9] = { aircraft type: 2; arco_tempo_order:  (1,3)-(1, 2); (3,1)-(2, 3); (1,1)-(3, 4); (1,1)-(4, 5); (1,1)-(5, 6); (1,1)-(6, 7); (1,1)-(7, 1);  COST: 25000360 }

我尝试为ConjCiclos添加一个不同的循环:

CICLO[10] = { aircraft type: 2; arco_tempo_order:  (2,3)-(1, 2); (3,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000140 }

正如我们所看到的,CICLO [10]是ConjCiclos的新CICLO,但它将CICLO [10]检测为冗余CICLO。

调试代码,我验证它使CICLO [10]的比较为:

obj1  = { aircraft type: 2 (1,4)-(1, 2); (4,1)-(2, 3); (1,1)-(3, 4); (1,1)-(4, 5); (1,1)-(5, 6); (1,1)-(6, 7); (1,1)-(7, 1);  COST: 25000228 }
obj2  = { aircraft type: 2 (2,3)-(1, 2); (3,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000140 }
7
obj1  = { aircraft type: 2 (2,5)-(1, 2); (5,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000164 }
obj2  = { aircraft type: 2 (2,3)-(1, 2); (3,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000140 }
7
obj1  = { aircraft type: 2 (1,5)-(1, 2); (5,1)-(2, 3); (1,1)-(3, 4); (1,1)-(4, 5); (1,1)-(5, 6); (1,1)-(6, 7); (1,1)-(7, 1);  COST: 25000142 }
obj2  = { aircraft type: 2 (2,3)-(1, 2); (3,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000140 }
7
obj1  = { aircraft type: 2 (4,5)-(1, 2); (5,4)-(2, 3); (4,4)-(3, 4); (4,4)-(4, 5); (4,4)-(5, 6); (4,4)-(6, 7); (4,4)-(7, 1);  COST: 25000096 }
obj2  = { aircraft type: 2 (2,3)-(1, 2); (3,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000140 }
1
obj1  = { aircraft type: 2 (2,3)-(1, 2); (3,2)-(2, 3); (2,2)-(3, 4); (2,2)-(4, 5); (2,2)-(5, 6); (2,2)-(6, 7); (2,2)-(7, 1);  COST: 25000140 }
obj2  = { aircraft type: 2 (1,5)-(1, 2); (5,1)-(2, 3); (1,1)-(3, 4); (1,1)-(4, 5); (1,1)-(5, 6); (1,1)-(6, 7); (1,1)-(7, 1);  COST: 25000142 }
7

如果我尝试更换运营商&lt;功能如下:

bool operator<(const CICLO& lhs, const CICLO& rhs) { return std::tie(lhs.COST, lhs.aircraftType, lhs.arco_tempo_order) < std::tie(rhs.COST, rhs.aircraftType, rhs.arco_tempo_order); }

同样的CICLO增加了很多时间。

例如,我在下面添加了两个CICLO:

CICLO[11499] = { aircraft type: 2 (3,2)-(3, 4); (2,1)-(4, 5); (1,5)-(5, 1); (5,5)-(1, 2); (5,5)-(2, 3); (5,5)-(3, 4); (5,3)-(4, 5); (3,3)-(5, 1); (3,3)-(1, 2); (3,3)-(2, 3);  COST: 46000392.0000000000 }
CICLOIT[11500] = { aircraft type: 2 (3,2)-(3, 4); (2,1)-(4, 5); (1,5)-(5, 1); (5,5)-(1, 2); (5,5)-(2, 3); (5,5)-(3, 4); (5,5)-(4, 5); (5,3)-(5, 1); (3,3)-(1, 2); (3,3)-(2, 3);  COST: 46000392.0000000000 }

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

运营商&lt;必须有一个严格的弱排序&#34;使其与STL一起正常工作。

本声明:

if (obj1.COST < obj2.COST - 1) {
    return true;
}

使条件不真实。

obj1.COST = 9;
obj2.COST = 10;

obj1 < obj2      false
obj2 < obj1      false

这意味着两个对象是相同的(所有其他事物都是相同的)。

让我们将它扩展为三个对象。

obj1.COST = 9;
obj2.COST = 10;
obj3.COST = 11;

obj1 < obj2      false
obj2 < obj1      false
// So obj1 == obj2


obj2 < obj3      false
obj3 < obj2      false
// So obj2 == obj3

// This we should be able to assume:
obj1 == obj3
// Otherwise strict weak ordering is not working.


obj1 < obj3      true
obj3 < obj1      false
// So they are not equal.
// Something is very wrong and thus your set is not going to work.

查看https://stackoverflow.com/a/37269108/14065的这个答案,以便轻松实现解决方案。