【问题标题】:Sorting list. What is wrong with my sorting method? [closed]排序列表。我的排序方法有什么问题? [关闭]
【发布时间】:2014-05-10 10:12:47
【问题描述】:

我对我的排序方法感到非常头疼。我不知道有什么问题?我已经检查了数百万次排序方法,但我仍然搞砸了。 这是我的代码

struct Man{
    string name;
    string adress;

    bool operator < (const Man & next);
};

bool Man::operator < (const Man & next){
    return adress < next.adress && name < next.name;
}

struct SarV{
    Man duom;
    SarV *sekV;
};

struct SarH{
    string date;
    SarH *sekH;
    SarV *prV;
};

void Branch::Check(string code, int month){
    ofstream rf("Rezultatai.txt", ios::app);
    rf.setf(ios::left);
    SarH *d = pr;
    rf << "Data" << endl;
    while(d != NULL){
        SarV *v = d->prV;
        Print(rf, v, code, month);
        d = d->sekH;
    }
    rf.close();
}

这是我的排序功能

void Branch::Sort(){
    string temp;
    for(SarH *s = pr; s != NULL; s = s->sekH){
        for(SarV *p = s->prV; p != NULL; p = p->sekV){
            for(SarV *p2 = p; p2 != NULL; p2 = p2->sekV){
                if(p2->duom < p->duom){
                    //---------------------------------------
                    temp = p->duom.name;
                    p->duom.name = p2->duom.name;
                    p2->duom.name = temp;
                    //---------------------------------------
                    temp = p->duom.adress;
                    p->duom.adress = p2->duom.adress;
                    p2->duom.adress = temp;
                    //---------------------------------------
                }
            }
        }
    }
}

那有什么问题呢?

【问题讨论】:

  • 欢迎来到 Stack Overflow!要求人们发现代码中的错误并不是特别有效。您应该使用调试器(或添加打印语句)来隔离问题,方法是跟踪程序的进度,并将其与您期望发生的情况进行比较。一旦两者发生分歧,你就发现了你的问题。 (然后如果有必要,你应该构造一个minimal test-case。)
  • 我说我的问题是排序方式。
  • @Mendinskis '我说我的问题是排序方法。'在这里问问题还不够。

标签: c++ list sorting


【解决方案1】:

您的运营商可能是错误的。

试试这个:

bool Man::operator < (const Man & next){
    if(adress < next.adress) return true;
    if(adress == next.adress) {
       if(name < next.name) return true;
    }
    return false;
}

【讨论】:

  • 非常感谢!它现在正在工作!我一直在检查我的排序方法谢谢!
猜你喜欢
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多