按字母顺序排序结构数组

时间:2019-12-03 05:36:45

标签: c++

所以我有一个带有各种变量的结构数组。

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
//--------------------------------------------------------------------------------------------------------------------------------------------------

如何使输出文件上打印的所有内容均按正确的信息按字母顺序排序?有人可以给我一些建议吗?

2 个答案:

答案 0 :(得分:0)

这取决于您如何定义飓风。如果它是C数组,那么您将需要这样的东西:

std::sort(hurricanes, hurricanes + counter, 
   [](const Data& a, const Data& b) { return std::strcmp(a.name, b.name) < 0; });

但是如果它是std :: vector或std :: array,则...

std::sort(hurricanes.begin(), hurricanes.end(), 
   [](const Data& a, const Data& b) { return std::strcmp(a.name, b.name) < 0; });

答案 1 :(得分:0)

我看不到您使用哪种类型的容器(我建议使用std :: vector),但是在此代码中,您可能会做一些不错的事情来使您的生活更轻松。

数据结构中的第一个重载运算符,例如:

  • ostream& operator<<(ostream& os, const Data& a_data)可轻松将您的结构发送到ostream(无需封装其所有成员)。
  • bool T::operator <(const T2 &b) const;bool T::operator >(const T2 &b) const;:用于在标准算法中进行比较。

看看参考文献:link您将只编写一次,但可以在程序的每个部分中使用它。

第二个建议是使用std :: sort算法对数据结构进行排序。

std::sort(hurricanes.begin(), hurricanes.end(), greater<Data>());

如果您在6个月左右的时间内重返项目,那么代码的语法将更容易理解和阅读。