2012-02-17 100 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()); 
}