【C++】vector 排序

1. 数组排序,元素为整数

// 打印函数
void Print(vector<int>& nums)
{
    std::cout << "Sorted numbers:" << std::endl;
    for (size_t i = 0; i < nums.size(); i++)
    {
        std::cout << nums[i];
        if (i != nums.size() - 1) {
            std::cout << ", ";
        }
    }
    std::cout << std::endl;
}

// 比较函数
bool Lower(int left, int right)
{
    return left <= right;
}

int main()
{
   // 正向取元素,默认升序
    vector<int> nums = { 5,3,1,2,4 };
    std::sort(nums.begin(), nums.end());
    Print(nums);
    // Sorted numbers:
    // 1, 2, 3, 4, 5
	
	// 反向取元素,默认升序,则实现降序效果
    std::sort(nums.rbegin(), nums.rend());
    Print(nums);
	// Sorted numbers:
	// 5, 4, 3, 2, 1
	
	// 自己编写排序规则
    std::sort(nums.begin(), nums.end(), Lower);
    Print(nums);
    // Sorted numbers:
    // 1, 2, 3, 4, 5
}

2. 数组排序,元素为类

  • 构造学生类
struct Student {
    string name;
    int score;
};
  • 打印函数
void PrintStudent(vector<Student> &students)
{
    std::cout << "Sorted students:" << std::endl;
    for (vector<Student>::const_iterator it = students.cbegin(); it != students.cend(); it++) {
        cout << "name: " << it->name << " score: " << it->score << endl;
    }
    cout << endl;
}
  • 排序函数
// 按名字升序排序
bool compName(const Student &lftStu, const Student &rhdStu)
{
    return lftStu.name <= rhdStu.name;
}

// 按分数升序排序
bool compScore(const Student& lftStu, const Student& rhdStu)
{
	// 这里注意,如果写小于等于就报错!!!
    return lftStu.score < rhdStu.score;
}

// 优先按分数由高到低排序,分数相同,按名字的字典序排序
bool compStudent(const Student& lftStu, const Student& rhdStu)
{
    if (lftStu.score > rhdStu.score) {
        return true;
    }
    else if (lftStu.score == rhdStu.score) {
        return lftStu.name <= rhdStu.name;
    } else {
        return false;
    }
}
  • 主函数,排序比较
int main()
{
	// 0 原始学生类
    vector<Student> students = {
        {"Tam",55},
        {"Tom",79},
        {"LiLei",85},
        {"Tomas",79},
        {"Randy",99}
    };

    // 1.默认sort排序,报错,原始排序无法排序
    std::sort(students.begin(), students.end());
    PrintStudent(students);

    // 2.按名称排序
    std::sort(students.begin(), students.end(),compName);
    PrintStudent(students);
	 //Sorted students :
    //name: LiLei score : 85
    //name : Randy score : 99
    //name : Tam score : 55
    //name : Tom score : 79
    //name : Tomas score : 79
    
    students = {
        {"Tam",55},
        {"Tom",79},
        {"LiLei",85},
        {"Tomas",79},
        {"Randy",99}
    };

    // 3.按分数升序排序
    std::sort(students.begin(), students.end(), compScore);
    PrintStudent(students);
    //Sorted students :
    //name: Tam score : 55
    //name : Tom score : 79
    //name : Tomas score : 79
    //name : LiLei score : 85
    //name : Randy score : 99
    
    students = {
        {"Tam",55},
        {"Tom",79},
        {"LiLei",85},
        {"Tomas",79},
        {"Randy",99}
    };

    // 4.优先按分数由高到低排序,分数相同,按名字的字典序排序
    std::sort(students.begin(), students.end(), compStudent);
    PrintStudent(students);
    //Sorted students :
    //name: Randy score : 99
    //name : LiLei score : 85
    //name : Tom score : 79
    //name : Tomas score : 79
    //name : Tam score : 55
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值