小编use*_*845的帖子

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

我正在编写一个程序,该程序将读取具有社会安全号码的名称列表(当然不是真实的名称),并根据命令行参数根据姓氏或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 = …
Run Code Online (Sandbox Code Playgroud)

c++ sorting stl operator-overloading stl-algorithm

2
推荐指数
1
解决办法
4867
查看次数

标签 统计

c++ ×1

operator-overloading ×1

sorting ×1

stl ×1

stl-algorithm ×1