2012-02-17 97 views
1

我正在编写一个程序,它将读取带有社会安全号码(当然不是真正的)的名称列表,并根据姓氏或ssn对列表进行排序,具体取决于在命令行参数上。为了简单起见,我已经超载了运算符以及超载的输入和输出操作符。一切都编译好,直到我添加排序功能和输出在主的末尾。我很难过。有任何想法吗?任何其他提示也非常感谢。重载比较运算符来处理C++中的STL排序

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

} 
+3

第一个问题:使用'='您要''==。你的编译器没有给你一个令人讨厌的警告吗? – 2012-02-17 22:56:05

+3

通常,列出您正在使用的编译器以及获取的确切错误消息很有帮助。 – 2012-02-17 23:00:09

+3

了解常量是否正确。没有必要将大多数参数作为非const的引用 – PlasmaHH 2012-02-17 23:16:31

回答

4

这是一种奇怪了,有事你编译器应该发出警告

if (rhs.t_sort = false) 

你是不是测试的t_sort价值,但始终设置它为false。

测试一个booltruefalse是有点不必要反正这是什么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; 
    } 
} 
+0

哇,我不能相信我错过了那个。我解决了这个问题,而且我也注意到我正试图输出一个矢量而不是一个记录。尽管我尝试使用排序算法,但仍遇到错误。我想我要么不正确地调用函数,要么在我的运算符<()函数中有错误。 – user1044845 2012-02-19 23:18:55

0

你肯定有你的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()); 
}