开发者社区> 问答> 正文

字母排序结构阵列

所以我有一个包含各种变量的结构数组。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 //-------------------------------------------------------------------------------------------------------------------------------------------------- 如何使输出文件上打印的所有内容都按照其正确的信息按字母顺序排序?有人能给我一些建议吗?

展开
收起
aqal5zs3gkqgc 2019-12-03 21:56:08 419 0
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; });

    2019-12-03 21:57:17
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载