按字母顺序排序c++
Alphabetically Sort 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;
}
我看到您直接使用每个方法(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::sortstd::sort(m_characters.begin(), m_characters.end());
相关文章:
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- std::sort()函数无法对向量的一部分进行排序
- shell排序中的交换和比较
- clang格式:禁用排序包含
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 试图在c++中对数字列表进行排序
- 如何在C++中对数组进行冒泡排序