C++ 字母排序结构数组

C++ 字母排序结构数组,c++,C++,所以我有一个包含各种变量的结构数组 struct Data { char name[11]; int ID; int life; int date; float avgWindSpeed; float avgRainFall; int tornadoes; int stormCategory; }; 我希望根据字符名[11]对所有信息进行排序。存储在struct data中的数据来自名为storms.txt的文件。目前,我有所有的

所以我有一个包含各种变量的结构数组

struct Data
{
    char name[11];
    int ID;
    int life;
    int date;
    float avgWindSpeed;
    float avgRainFall;
    int tornadoes;
    int stormCategory;
};
我希望根据
字符名[11]
对所有信息进行排序。存储在
struct data
中的数据来自名为
storms.txt
的文件。目前,我有所有的东西要完美地组合,但仍在努力按字母顺序排序。另外,我将我的
of Stream outfile
命名为
,并且
计数器
包含文件中风暴的总数。
我目前的代码是:

//--------------------------------------------------------------------------------------------------------------------------------------------------
//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
//--------------------------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------------------------
//一级飓风开始
int=1=0//将保持1级风暴的数量
//这只是设置图表的顶部

outfile这取决于你如何定义飓风。如果它是一个C数组,那么您将需要如下内容:

std::排序(飓风、飓风+计数器、,
[](constdata&a,constdata&b){返回std::strcmp(a.name,b.name)<0;};
但是,如果它是std::vector或std::array,则

排序(hurricanes.begin(),hurricanes.end(), [](constdata&a,constdata&b){返回std::strcmp(a.name,b.name)<0;};
我不知道您使用什么类型的容器(我建议使用std::vector),但是您可以做一些很好的事情,让您在这段代码中生活得更轻松

首先重载数据结构中的运算符,如:

  • ostream&operator查看。一开始它有点神秘,但实际上你可以按照指定编写一个
    comp
    (compare)函数,或者使用一个lamba函数或是一个容器,例如
    std::array
    std::sort(hurricanes.begin(), hurricanes.end(), greater<Data>());