• c++程序—结构体排序函数


    #include<iostream>
    using namespace std;
    #include<string>
    //结构体数组
    struct hero 
    {
        string name;
        int age;
        string sex;
    };
    
    void bubblesort(hero arr[],int len) {
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (arr[j]. age > arr[j+1].age) {
                    struct hero temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    
    void printhero(hero arr[],int len) {
        for (int i = 0; i < len; i++) {
            cout << "	" << arr[i].name << "	" << arr[i].age << "	" << arr[i].sex << endl;
    
        }
    }
    int main()
    {
        hero heroArry[5] = {
            {"刘备",23,""},
            {"关羽",18,""},
            {"张飞",20,""},
            {"赵云",16,""},
            {"黄月英",19,""}
        };
    
        //输出结构体数组
        int len = sizeof(heroArry) / sizeof(heroArry[0]);
        //for (int i = 0; i < len; i++) {
        //    cout << heroArry[i].name << "	" << heroArry[i].age << "	" << heroArry[i].sex << endl;
    
        //}
        cout << "按照年龄排序前的顺序为:" << endl;
        printhero(heroArry, len);
    
        bubblesort( heroArry,len);
    
        cout << "按照年龄排序后的顺序为:" << endl;
        printhero(heroArry, len);
    
    
        system("pause");
        return 0;
    
    }
  • 相关阅读:
    Android学习——SAX解析方式
    Android学习——pull解析方式
    Android学习——使用okhttp
    开课第十五周周总结
    顶会热词3
    顶会热词2
    顶会热词1
    配置JAVA环境
    配置mybaits的一些坑
    Maven配置和idea种tomcat配置的一些坑点
  • 原文地址:https://www.cnblogs.com/hackerteen/p/12466567.html
Copyright © 2020-2023  润新知