按字母顺序排序C ++

时间:2016-10-21 06:14:23

标签: c++ parsing alphabetical-sort

我有一个以某种方式组织的视频游戏角色列表。 我希望能够从列表中取出他们的名字并按字母顺序排序。

列表格式为:

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 个答案:

答案 0 :(得分:0)

我看到你直接使用每个方法的文件(getLast()等)。这不是最好的方法,因为访问文件成本高,效率低。

您应该在内存中构造文件的表示形式:首先将每一行表示为Character类,例如:

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

打开文件,读取每一行,使用解析后的字符串构造一个Character(用逗号分隔)。

正如tadman在评论中提出的那样,您可以使用std::sort方法按名称命名字符。实施运营商&lt;在Character类中,例如:

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

因此,您可以在vector<Character> m_characters

上使用std :: sort
std::sort(m_characters.begin(), m_characters.end());