Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载比较运算符以使用C+中的STL排序+;_C++_Sorting_Stl_Operator Overloading_Stl Algorithm - Fatal编程技术网

C++ 重载比较运算符以使用C+中的STL排序+;

C++ 重载比较运算符以使用C+中的STL排序+;,c++,sorting,stl,operator-overloading,stl-algorithm,C++,Sorting,Stl,Operator Overloading,Stl Algorithm,我正在编写一个程序,它将读取带有社会安全号码(当然不是真实的)的姓名列表,并根据命令行参数根据姓氏或ssn对列表进行排序。为了简单起见,我重载了 P>你确信对于记录< /Cord>类有一个真正的意义,不仅用于任意排序的目的?考虑一个大整数的类,其中一个这样的排序对于你的对象是有意义的,但是它会对你的记录产生这样的有效意义吗?或者如果你不排序,它会失去它的意义吗?> [imo]不要将操作符)应通过常量引用获取记录参数(它们不应更改接收到的对象).哇,我简直不敢相信我错过了。我修复了它,还注意到我试

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

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

}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
枚举sortVar{NAME,SOCSEC};
课堂记录{
公众:

friend bool运算符这有点奇怪,编译器应该对此提出警告

if (rhs.t_sort = false)
您不是在测试
t\u sort
的值,而是总是将其设置为false

不管怎样,根据
true
false
测试
bool
是有点不必要的,因为这是
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;
    }
}

<代码> BoOL运算符> P>你确信对于<代码>记录< /Cord>类有一个真正的意义,不仅用于任意排序的目的?考虑一个大整数的类,其中一个这样的排序对于你的对象是有意义的,但是它会对你的记录产生这样的有效意义吗?或者如果你不排序,它会失去它的意义吗?> [imo]不要将
操作符r.x;});
排序(foos.begin(),foos.end(),foosensioning());
}

第一个问题:在你想要的地方使用
=
。你的编译器没有给你一个严重的警告吗?一般来说,列出你正在使用的编译器和你得到的确切错误消息是很有帮助的。了解常量是否正确。你的大多数参数不需要引用非常量。你应该开始使用
>const
在代码中多一些…比较和插入操作符(
操作符>
)应通过常量引用获取
记录
参数(它们不应更改接收到的对象).哇,我简直不敢相信我错过了。我修复了它,还注意到我试图输出一个向量而不是一个记录。但是,当我尝试使用排序算法时,仍然会出现错误。我想我要么调用的函数不正确,要么我的运算符有错误
#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());
}