所以我有一个包含各种变量的结构数组。struct Data { char name[11]; int ID; int life; int date; float avgWindSpeed; float avgRainFall; int tornadoes; int stormCategory; }; 我想对所有的信息进行排序char name[11]...中存储的数据。struct Data来自名为storms.txt...目前,我所有的东西都要做得很完美,但仍在努力按字母顺序排序。另外,我把我的名字命名为ofstream outfile和counter包含文件中风暴的总数。 我目前的代码是:
//-------------------------------------------------------------------------------------------------------------------------------------------------- //Start of Hurricane Level 1 int totalLevel1 = 0; //Will hold the number of storms that are level 1
//This is just setting the top part of the chart
outfile << setw(70) << "Hurricane Level 1" << endl << endl;
outfile << "Name" << setw(10) << "ID" << setw(20) << " Life " << setw(20) << " Average " << setw(20) << " Average " << setw(20) << "Tornadoes" << setw(19) << " Date " << endl;
outfile << " " << setw(10) << " " << setw(20) << "in days" << setw(20) << "wind speed" << setw(20) << "rain fall" << setw(20) << " spawned " << setw(20) << " " << endl;
outfile << endl << endl;
float avgLifeSpan, avgRainFall, avgTornadoes, avgWindSpeed, life = 0, rain= 0, tornado= 0, wind= 0;
//Starting to process the information and printing it in its proper location
for(int i = 0; i < counter; i++)
if(hurricanes[i].stormCategory == 1)
{
totalLevel1++;
life = life + hurricanes[i].life;
rain = rain + hurricanes[i].avgRainFall;
tornado = tornado + hurricanes[i].tornadoes;
wind = wind + hurricanes[i].avgWindSpeed;
outfile << hurricanes[i].name << setw(5) << hurricanes[i].ID << setw(15) << hurricanes[i].life << setw(21) << hurricanes[i].avgWindSpeed
<< setw(20) << hurricanes[i].avgRainFall << setw(19) << hurricanes[i].tornadoes << setw(21) << hurricanes[i].date << endl;
}
//Printing the extra information for HURRICANE LEVEL 1
outfile << endl << endl << "Total number of Level 1 hurricanes is " << totalLevel1 << "." << endl;
outfile << "Average Life span in days of Level 1 hurricanes is " << life / float(totalLevel1) << "." << endl;
outfile << "Average rain fall for Level 1 hurricanes is " << rain / float(totalLevel1) << "." << endl;
outfile << "Average tornadoes spawned for Level 1 hurricanes is " << tornado / float(totalLevel1) << "." << endl;
outfile << "Average wind speed for Level 1 hurricanes is " << wind / float(totalLevel1) << "." << endl;
outfile << endl << endl;
//End of the Hurricane Level 1 //-------------------------------------------------------------------------------------------------------------------------------------------------- 如何使输出文件上打印的所有内容都按照其正确的信息按字母顺序排序?有人能给我一些建议吗?
这取决于你如何定义hurricanes。如果它是一个C数组,那么您需要这样的东西:
std::sort(hurricanes, hurricanes + counter, [](const Data& a, const Data& b) { return std::strcmp(a.name, b.name) < 0; }); 但是,如果它是std::Vector或std::数组,那么.
std::sort(hurricanes.begin(), hurricanes.end(), [](const Data& a, const Data& b) { return std::strcmp(a.name, b.name) < 0; });
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。