按字母顺序排序c++

Alphabetically Sort C++

本文关键字:排序 c++ 顺序      更新时间:2023-10-16

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

列表的格式为:

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

我看到您直接使用每个方法(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;
};

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

正如tadman在注释中提出的那样,您可以使用std::sort方法按名称对字符进行排序。实现运算符<在字符类中,例如:>

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());