#include <iostream>
#include <vector>
#include <algorithm> // 包含sort函数
using namespace std;
int main() {
// 创建一个整数向量
vector<int> numbers = {5, 2, 9, 1, 5, 6};
// 使用sort函数对向量进行排序
sort(numbers.begin(), numbers.end());
// 输出排序后的结果
cout << "Sorted numbers: ";
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
return 0;
}
#include <algorithm> 是为了使用 std::sort 函数。vector<int>,并初始化了一些数值。sort 函数:sort(numbers.begin(), numbers.end()) 对 numbers 向量中的元素进行升序排序。begin() 和 end() 分别返回向量的起始和结束迭代器。for 循环遍历并输出排序后的向量元素。如果你需要对自定义类型或使用不同的排序规则(如降序),可以传递第三个参数作为比较函数。
上一篇:switch c++
下一篇:c++友元函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站