【问题标题】:How can I define the operator< for a struct?如何为结构定义 operator<?
【发布时间】:2018-04-09 20:27:51
【问题描述】:

我有结构:

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

我需要将 operator

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 中插入了一些初始 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

如果我尝试将 operator

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 }

有人知道为什么会这样吗?

【问题讨论】:

  • 欢迎来到 Stack Overflow。你能给我们举一个两个 CICLO 的例子,运算符应该返回什么,它实际上返回什么?
  • 只需使用类似:bool operator&lt;(const CICLO&amp; lhs, const CICLO&amp; rhs) { return std::tie(lhs.COST, lhs.aircraftType, lhs.arco_tempo_order) &lt; std::tie(rhs.COST, rhs.aircraftType, rhs.arco_tempo_order); }
  • 即使 COST、aircraftType 和 arco_tempo_order.size() 相同,我也需要验证 arco_tempo_order 中的每个 ARCO_TEMPO 以确定两个 CICLO 是否相同。所以,我认为我不能只使用你所说的,@Jarod42!
  • operator &lt; (const std::set&lt;T&gt;&amp;, const std::set&lt;T&gt;&amp;) 已定义。
  • 当我尝试用你的操作符替换我的操作符时,会多次生成相同的 CICLO。我认为由于 COST 是一个浮点变量,并且我正在使用大小值来计算 COST,因此我需要在原始 COST 中添加一些小值。我认为由于 COST 值的微小差异,它正在考虑将相同的 CICLOS 视为不同的 CICLOS。我尝试在 rhs.COST 中添加 1,但随后出现错误消息。

标签: c++ struct operators


【解决方案1】:

运算符

此声明:

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 的此答案,以便轻松实施解决方案。

【讨论】:

  • 问题是我对 COST 的大值感到厌烦。然后,我不会有一个成本差异的CICLOS。我试图将我的 operator
  • @LuizaReal 基本上你需要以一种一致的方式定义小于。你的方法不一致。您需要考虑另一种方法(从 rhs 中减去 1 是行不通的)。比较浮点值总是很棘手(尽量避免这种情况(即强制转换为整数)。
  • 谢谢你,@MartinYork!我将 float 替换为 double 并且效果很好。
猜你喜欢
  • 2011-04-22
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多