【问题标题】:Using the function "find", of a set of struct, by passing just one attribute of the struct?通过仅传递结构的一个属性来使用一组结构的函数“查找”?
【发布时间】:2018-03-19 00:36:52
【问题描述】:

是否可以通过仅传递结构的一个属性来使用一组结构的“查找”函数?

结构体和操作符

struct ARCO_TEMPO {
    Arco a;
    int slotTimeU; // momento que sai do vertice u do arco
    int slotTimeV; // momento que chega no vertice v do arco
    int order;
    ARCO_TEMPO () {};
    ARCO_TEMPO (const ARCO_TEMPO& obj): a(obj.a), slotTimeU(obj.slotTimeU), slotTimeV(obj.slotTimeV), order(obj.order) {};
    ARCO_TEMPO (Arco _a, int _slotTimeU, int _slotTimeV, int _order) : a(_a), slotTimeU(_slotTimeU), slotTimeV(_slotTimeV), order(_order) {}    

};

struct PATH {

    set<ARCO_TEMPO> rota;
    float COST;

};

bool operator<(const ARCO_TEMPO& obj1, const ARCO_TEMPO& obj2) {
    if (obj1.slotTimeU < obj2.slotTimeU) {
        return true;
    }
    else {
        if (obj1.slotTimeU == obj2.slotTimeU && obj1.slotTimeV < obj2.slotTimeV) {
            return true;
        }
        else {
            if (obj1.slotTimeU == obj2.slotTimeU && obj1.slotTimeV == obj2.slotTimeV && obj1.a.i < obj2.a.i) {
                return true;
            }
            else {
                if (obj1.slotTimeU == obj2.slotTimeU && obj1.slotTimeV == obj2.slotTimeV &&obj1.a.i == obj2.a.i && obj1.a.j < obj2.a.j ) {
                    return true;
                }
                else{ 
                    return false;
                }
            }
        }

bool operator<(const PATH& obj1, const PATH& obj2) {
    if (obj1.COST < obj2.COST) {
        return true;
    }
    else {
        if (obj1.COST == obj2.COST && obj1.rota < obj2.rota) {
            return true;
        }
        else{ 
            return false;
        }
    }
}

我有一个变量 PATH,称为 aux_p。然后我有这个PATH(aux_p.rota)的属性rota。我想在set rota(aux_p.rota)中找到“位置”,其中的属性顺序等于一个特定的值。

我尝试了以下几行,但它不起作用:

set<ARCO_TEMPO>::iterator it;
int orderAux = aux_p.rota.size();
it = aux_p.find(it->orderAux)

如何使用第二个容器 unordered_map 来完成这项工作?
有人可以帮我吗?

【问题讨论】:

  • 使用未初始化的迭代器:it-&gt;orderAux.
  • 请务必提供minimal reproducible example。当前示例无效:ARCO_TEMPO::orderAux 未声明,PATH::find 未声明。

标签: c++ struct find set


【解决方案1】:

你想要的都是不可能的。遍历从begin()end() 的所有集合元素,或者使用第二个容器unordered_map&lt;int&gt;

顺便说一句,ARCO_TEMPO::orderAuxPATH::find 未声明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多