将数据从文件读入结构体,对数据进行排序并写入文件
Reading data from a file into a struct, sorting the data and writing to a file
我几乎什么都试过了。只是想找点建议。
该项目是从文件["racers2011.txt"]中读取数据到一个结构体中,并对男性和女性的比赛时间进行排序。将雄性和雌性分开分组,并输出它们的等级和比赛时间,作为它们最好的蓝色种族和它们最好的红色种族的总和。我已经读取文件并输出到新文件,但不知道如何对文件进行排序。
如果有人能帮我一点忙,我将不胜感激。
这是我到目前为止的代码(我有一些代码不编译,所以我已经把它注释掉了):
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
struct Racer_struct
{
int bib;
char sex;
char fname[30];
char lname[30];
double b1, b2, r1, r2;
};
bool connectInFile(ifstream& fin, char infilename[]);
bool connectOutFile(ofstream& fout, char outfilename[]);
void readData(ifstream& fin, Racer_struct racers[], const int& MAX);
//void racerGender(ostream& fout, Racer_struct racers[], const int& MAX);
//double calcTotalTime(Racer_struct racers[], double total[], const int& MAX);
void writeData(ostream& fout, Racer_struct racers[], const int& MAX);
int main()
{
const int MAX = 38;
Racer_struct racers[MAX];
// double total[MAX];
ifstream fin;
ofstream fout;
char in_file[30], out_file[30];
bool opened;
char title[79];
opened = connectInFile(fin, in_file);
cout << opened << endl;
opened = connectOutFile(fout, out_file);
cout << opened << endl;
if(opened)
{
cout << "CONNECTED to: " << in_file << endl;
cout << "WRITING to: " << out_file << endl;
for(int i=0; i<=3; i++)
{
fin.getline(title, 80);
fout << title << "n";
}
}
readData(fin, racers, MAX);
writeData(fout, racers, MAX);
fin.close();
fout.close();
cout << endl;
return 0;
}
bool connectInFile(ifstream& fin, char infilename[])
{
bool success = true;
cout << "Enter input filename: ";
cin >> infilename;
fin.open(infilename);
if(fin.fail())
success = false;
return success;
}
bool connectOutFile(ofstream& fout, char outfilename[])
{
bool opened = true;
cout << "Enter the filename you wish to write to: ";
cin >> outfilename;
fout.open(outfilename);
if(fout.fail())
opened = false;
return opened;
}
void readData(ifstream& fin, Racer_struct racers[], const int& MAX)
{
char ws;
for(int i=0; i<MAX && fin.peek()!= EOF; i++)
{
fin >> racers[i].bib >> racers[i].sex >> racers[i].fname >> racers[i].lname
>> racers[i].b1 >> racers[i].b2 >> racers[i].r1 >> racers[i].r2;
fin.get(ws);
}
}
/*
void racerGender(ostream& fout, Racer_struct racers[], const int& MAX)
{
for(int i=0; i<MAX; i++)
if(racers[i].sex == 'M')
{
calcTotalTime(racers, total, MAX);
writeData(fout, racers, MAX);
}
else
{
calcTotalTime(racers, total, MAX);
writeData(fout, racers, MAX);
}
}
double calcTotalTime(Racer_struct racers[], double total[], const int& MAX)
{
double total[MAX];
for(int i=0; i<MAX; i++)
if(racers[i].r1 > racers[i].r2 && racers[i].b1 > racers[i].b2)
total[i] = racers[i].r2 + racers[i].b2;
else if(racers[i].r2 > racers[i].r1 && racers[i].b2 > racers[i].b1)
total[i] = racers[i].r1 + racers[i].b1;
else if(racers[i].r1 > racers[i].r2 && racers[i].b2 > racers[i].b1)
total[i] = racers[i].r2 + racers[i].b1;
else
total[i] = racers[i].b2 + racers[i].r1;
return total[i];
}
*/
void writeData(ostream& fout, Racer_struct racers[], const int& MAX)
{
for(int i=0; i<MAX; i++)
{
fout << racers[i].bib << "t" << racers[i].sex << "t" << racers[i].fname
<< "t" << racers[i].lname << "t" << racers[i].b1 << "t" << racers[i].b2
<< "t" << racers[i].r1 << "t" << racers[i].r2 /*<< "t" << total[i]*/ << endl;
/* if((i+1)%5)
fout << "t";
else
fout << endl;
*/
}
}
使用std::sort。说得太多会暴露的,我想这是作业。
std::sort
是一个非常有效的排序函数,它是c++标准的一部分,在标题algorithm
中。
std::sort
使用了"迭代器"的概念。这是一个比较难的主题,所以我将在这里大致总结一下。在c++中,任何序列都可以表示为一对迭代器:一个指向第一个元素,第二个指向最后一个元素之后的一个位置(因此,[begin, end[
)。这在数组中很容易看到:对于大小为N
的数组a
, a[N]
不是数组的一部分。数组的迭代器类型为指针。
std::sort(racers, racers + MAX);
上面这行可以理解为"对racers
和racers + MAX
所分隔的序列中的元素进行排序"。对于数组,数组的名称指向第一个元素,并将该地址的大小添加到"end"迭代器(如上所述)。如果使用标准容器,如std::vector
,则使用vector的begin()
和end()
方法来获得相应的迭代器。
现在,std::sort
使用比较函数逐个比较每个元素。默认情况下,这是<
操作符(因此元素按升序排序)。重载允许您在需要时提供自己的函数。在我们的例子中,为Racer_struct
重载<
就足够了:
// This should be defined after Racer_struct and before the first call to std::sort
bool operator<(const Racer_struct &left, const Racer_struct &right)
{
// return true if left should get before right
}
正如John Zwinck所说,您可能希望使用std::sort
来进行排序。就我个人而言,我将operator>>
和operator<<
过载为读写。我还会重载operator<
来进行比较。
有了这些,你的顶层代码可能看起来像这样:
typedef std::istream_iterator<Racer_struct> reader;
std::vector<Racer_struct> racers((reader(fin)), reader());
std::sort(racers.begin(), racers.end());
std::copy(racers.begin(), racers.end(),
std::ostream_iterator<Racer_struct>(std::cout, "n"));
考虑到你的标准(把男性和女性分开),你可能想把性别作为主要领域,然后是时间。这将把所有的雄性和所有的雌性组合在一起(按照你选择的顺序)。
- 如何对点云数据进行排序
- 如何使用 QSortFilterProxyModel::sort 对 Qlist 中的数据进行排序
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 使用选择排序对数组数据结构进行排序,但它不起作用
- 为什么我的代码没有对数组中第二个索引上的数据进行排序?
- 使用 QSortFilterProxyModel 对 Qml ListView 的数据进行排序
- 读取 C++ 中的 txt 数据,展开为行或列并排序
- C++ 从文件中获取数据时使用 strcpy 和 strcmp 按字母顺序对数组进行排序?
- 存储在 std::map/std::set 中,与在存储所有数据后对向量进行排序
- 尝试创建排序的动态列表(数据结构)
- 固定大小的容器,其中元素被排序,并可以为C 中的数据提供原始指针
- 排序算法,使用模板按内部数据对对象进行排序
- 对多个数据成员进行排序
- 递归定义数组中的数据对齐和排序
- 如何修复输出显示,以便显示正确排序的数据
- 为什么我的气泡排序不适用于双精度数据类型?
- 气泡排序不传递数据或不起作用
- 使用 stl::map 和 stl::unordered_map 对包含大量重复元素的数组数据进行排序
- 数据排序依据是哪一列,QTableWidget
- 寻找更好的数据排序方法