#include <iostream>
#include <vector>
#include <algorithm> // 包含sort函数
using namespace std;
int main() {
    // 定义一个整数向量
    vector<int> nums = {5, 2, 9, 1, 5, 6};
    // 使用sort函数对向量进行排序,默认为升序排序
    sort(nums.begin(), nums.end());
    // 输出排序后的结果
    cout << "Sorted numbers: ";
    for (int num : nums) {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}#include <algorithm> 是必须的,因为 sort 函数定义在这个头文件中。std::vector<int> 来存储一组整数。sort 函数:sort(nums.begin(), nums.end()) 对向量 nums 中的元素进行排序。默认情况下,sort 函数会按照升序排列。如果你需要对自定义类型或按特定规则排序,可以传递第三个参数作为比较函数。
上一篇:c++ sort
下一篇:bits/stdc++.h
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站