2016-10-21 583 views
0

我有一個以特定方式組織的視頻遊戲角色列表。 我希望能夠僅從名單中取出他們的名字並按字母順序排序。按字母順序排序C++

名單由格式化:

Last Name, First Name, Game, Relase Date, Score, Developer 

名單是:

Snake, Solid, Metal Gear Solid, 9/3/1998, 94, Konami 
Drake, Nathan, Uncharted, 11/19/2007, 90, Naughty Dog 
Guy, Doom, Doom, 5/13/1993, 95, iD 

我想輸出是:

Drake, Nathan 
Guy, Doom 
Snake, Solid 

我可以在只打印有名字他們在文本文件中的順序。我如何比較姓氏,然後打印出全名?

這是到目前爲止我的代碼:

#include <fstream> 
#include <iostream> 
#include <string> 
#include <time.h> 
#include <cstdlib> 
#include <sstream> 
using namespace std; 

ifstream inFile; 

class Characher{ 
private: 
    string first; 
    string last; 
public: 
    Character(){}; 

    void getLast(){ 
    if(inFile.is_open()){ 
     getline(inFile, last,','); 
    } else { 
    cout<<"Hmm.."<<endl; 
     } 
    } 

    void getFirst(){ 
    if(inFile.is_open()){ 
     getline(inFile, first,','); 
    } else { 
    cout<<"No First Name here..."<<endl; 
     } 
    } 

    void printLast(){ 
    cout<<last<<","; 
    } 

    void printFirst(){ 
    cout<<first<<endl; 
    } 
}; 

class Dev{ 
private: 
    string Developer; 
public: 
    Dev(){};//null constructor 

    void printDeveloper(){ 
    cout<<"Developer: "<<Developer<<endl; 
    } 

    void getDeveloper(){ 
    if(inFile.is_open()){ 
     getline(inFile, Developer); 
    } else { 
    cout<<"Nothing here..."<<endl; 
    } 
    } 
}; 
class Game{ 
private: 
    string game; 
public: 
    Game(){}; 

    void getGameName(){ 
    if(inFile.is_open()){ 
     getline(inFile, game,','); 
     } else{ 
     cout<<"What game was they frum?"<<endl; 
     } 
    } 

    void printGame(){ 
    cout<<"Game: "<<game; 
    } 

}; 

class RelDate_And_Score{ 
private: 
    string ReleaseDate; 
    string Score; 
public: 
    RelDate_And_Score(){}; 

    void GetRelDate(){ 
    if(inFile.is_open()){ 
     getline(inFile, ReleaseDate, ','); 
    } else{ 
    cout<<"Could not find Release Date"<<endl;} 
    } 

    void getScore(){ 
    if(inFile.is_open()){ 
     getline(inFile, Score, ','); 
    } else{ 
    cout<<"Could not find Score"<<endl;} 
    } 

    void PrintDate(){ 
    cout<<"Release Date: "<<ReleaseDate<<" | ";} 

    void PrintScore(){ 
     cout<<"Score: "<<Score<<endl;} 

}; 

int main(){ 
    inFile.open("Games.dat"); 

    Dev d; 
    Characher c; 
    RelDate_And_Score r; 
    Game g; 

    for (int i=0; i<3; i++) 
    { 
     c.getLast(); 
     c.getFirst(); 
     g.getGameName(); 
     r.GetRelDate(); 
     r.getScore(); 
     d.getDeveloper(); 
     c.printLast(); 
     c.printFirst(); 
    } 

    return 0; 
} 
+1

在std ::排序你的意思['標準:: sort'(HTTP: //en.cppreference.com/w/cpp/algorithm/sort)?在你的對象上定義'operator <'或者寫一個'Compare'函數來比較它們。 – tadman

+0

您無法更改文件中的順序,因此您需要在內存中創建副本:創建地圖,將每一行添加到地圖並將名稱作爲關鍵字。在地圖上的循環會將它們顯示爲排序 – Lanting

+0

您的設計具有不同的角色,遊戲,日期等類別,每個類打開並訪問全局文件對象,比所需的更復雜,並且可能無法正常工作,因爲這些類會通過讀取同一行來干擾。每行有一個類實例會更容易,字符,遊戲等字符串成員,將這些類的實例放入向量中(不要在循環中使用數字3,保持靈活性),然後在矢量上使用'std :: sort'。 –

回答

0

我看你直接與每個方法(getLast()等)的文件工作。這不是最好的方式,因爲訪問該文件代價高昂且效率較低。

你應該建立在內存中的文件的表示:開始代表每行作爲一個字符類,如:

class Character 
{ 
    public: 
    Character(const string & firstname, const string & secondname, const string & game, const string & releasedate, const string & score, const string & developer) 
    : 
    m_firstname(firstname), m_secondname(secondname), m_game(game), m_releasedate(releasedate), m_score(score), m_developer(developer) 
    {} 

    private: 

    string m_firstname, m_secondname, m_game, m_releasedate, m_score, m_developer; 
}; 

打開文件,讀取每一行,使用解析字符串構成文字(以逗號分割)。

正如tadman在評論中提出的那樣,您可以使用std::sort方法按名稱排序字符。實現運營商< Character類如:

class Character 
{ 
    //... 
    bool operator<(const Character & c) 
    { 
     return m_firstname < c.m_firstname; 
    } 
    //... 
}; 

所以,你可以用你的vector<Character> m_characters

std::sort(m_characters.begin(), m_characters.end());