2

我正在编写一个程序,它将读取带有社会安全号码(当然不是真实号码)的姓名列表,并根据命令行参数根据姓氏或 ssn 对列表进行排序。为了简单起见,我重载了 < 运算符,还重载了输入和输出运算符。一切编译正常,直到我在 main 末尾添加排序函数和输出。我难住了。有任何想法吗?任何其他提示也非常感谢。

#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;

enum sortVar { NAME, SOCSEC };

class record {
    public:
        friend bool operator<(record& rhs, record& name);
        friend ostream& operator<<(ostream& out, record& toWrite);
        friend istream& operator>>(istream& in, record& toRead);
        bool t_sort;    
    private:
        string firstName, lastName, ssn;

};

bool operator<(record& rhs, record& next)
{
    if (rhs.t_sort = false) {
        if (rhs.lastName == next.lastName)
            return rhs.firstName < next.firstName;
        else
            return rhs.lastName < next.lastName;
    }
    else if (rhs.t_sort = true)
        return rhs.ssn < next.ssn;
}

ostream& operator<<(ostream& out, record& toWrite)
{
    out << toWrite.lastName 
         << " " 
         << toWrite.firstName 
         << "    " 
         << toWrite.ssn;
}

istream& operator>>(istream& in, record& toRead)
{
    in >> toRead.lastName >> toRead.firstName >> toRead.ssn;
}

int main(int argc, char* argv[])
{
    if (argc !=3) {
        cerr << "Incorrect number of arguments.\n";
        exit(1);
    }
    if (argv[1] != "name" || argv[1] != "socsec") {
        cerr << "Argument 1 must be either 'name' or 'socsec'.\n";
        exit(1);
    }

    sortVar sortMode;
    if (argv[1] == "name")
        sortMode = NAME;
    else if (argv[1] == "socsec")
        sortMode = SOCSEC;

    ifstream fin(argv[2]);

    vector<record> nameList;
    while(!fin.eof()) {
        record r;
        if (sortMode == NAME)
            r.t_sort = false;
        else if (sortMode == SOCSEC)
            r.t_sort = true;
        fin >> r;
        nameList.push_back(r);
    }

    //sort(nameList.begin(), nameList.end());
    //cout << nameList;

}
4

2 回答 2

5

这有点奇怪,你的编译器应该警告

if (rhs.t_sort = false)

您不是在测试 的值,t_sort而是始终其设置为 false。

无论如何,测试bool反对trueorfalse有点不必要,因为这是if- 语句已经在做的事情。

试试这个代码

bool operator<(const record& rhs, const record& next)
{
    if (rhs.t_sort) {
        return rhs.ssn < next.ssn;
    }
    else
    {
        if (rhs.lastName == next.lastName)
            return rhs.firstName < next.firstName;
        else
            return rhs.lastName < next.lastName;
    }
}
于 2012-02-18T07:05:33.990 回答
1

您确定为您的record班级排序具有真正的意义,而不仅仅是为了任意排序目的吗?考虑一个用于大整数的类,其中对您的对象进行这样的排序是有意义的,但它对您的记录是否有意义?或者如果你从不排序,它会失去意义吗?

[imo] 不要将这个由引入的排序operator<与你的类定​​义结合起来,除非它与它有真正的对应关系,换句话说,如果人类直观地清楚某些“对象 a”小于某个“对象 b”。

如果您想对非直观排序的类对象进行不同的排序,则尤其如此,请考虑按名字、姓氏、汽车数量等升序与降序。然后没有人会在不查找文档/代码的情况下记住您的默认顺序,从而失去它的便利性。

相反,要么传递函子,要么就地 lambda 函数:

#include <algorithm>
#include <functional>
#include <vector>

struct Foo {
    int x;
    Foo (int x) : x(x) {}
};

struct FooAscending : std::binary_function<Foo,Foo, bool>
{
    bool operator() (Foo const &lhs, Foo const &rhs) const { 
        return lhs.x < rhs.x;
    }
};

int main () {
    std::vector<Foo> foos;
    foos.emplace_back(1);
    foos.emplace_back(2);

    sort (foos.begin(), foos.end(), 
          [](Foo const &l, Foo const &r) { return l.x < r.x; });
    sort (foos.begin(), foos.end(), 
          [](Foo const &l, Foo const &r) { return l.x > r.x; });

    sort (foos.begin(), foos.end(), FooAscending());
}
于 2012-02-21T15:47:18.767 回答