重载比较运算符以使用C ++中的STL排序 [英] Overloading comparison operators to work with STL sort in C++

查看:133
本文介绍了重载比较运算符以使用C ++中的STL排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,它将读取社会安全号码(当然不是真实的)的名称列表,并根据名字或ssn,根据命令行参数排序列表。我已经重载了<操作符以及重载的输入和输出操作符。一切编译良好,直到我添加排序函数和输出在main结尾。我被骗了。有任何想法吗?任何其他提示也非常感谢。

I am writing a program that will read in a list of names with social security numbers (not real ones of course) and sort the list based on either last name or ssn, depending on a command line argument. I have overloaded the < operator and also overloaded input and output operators for simplicity. Everything compiles fine until I add the sort function and the output at the end of main. I'm stumped. Any ideas? Any other tips are also greatly appreciated.

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

}


推荐答案

这是有些奇怪,你的编译器应警告的事情。

This is kind of strange, and something your compiler should warn about

if (rhs.t_sort = false)

您不是测试 t_sort 的值, / em>它为false。

You are not testing the value of t_sort but always setting it to false.

针对 true 或<$ c测试 bool $ c> false 有点不必要,因为这是如果 -statement已经做了。

Testing a bool against true or false is bit unnecessary anyway, as this is what the if-statement is doing already.

尝试此代码

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

这篇关于重载比较运算符以使用C ++中的STL排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆