#include <iostream>
#include <vector>
#include <algorithm>
int main() {
// 创建一个整数向量
std::vector<int> numbers = {5, 2, 9, 1, 5, 6};
// 使用 std::sort 对向量进行排序
std::sort(numbers.begin(), numbers.end());
// 输出排序后的结果
std::cout << "Sorted numbers: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
#include <algorithm>
:包含标准库中的算法头文件,std::sort
函数就定义在这个头文件中。std::vector<int> numbers
:创建一个整数类型的向量,并初始化一些数据。std::sort(numbers.begin(), numbers.end())
:调用 std::sort
函数对向量 numbers
进行排序。begin()
和 end()
分别返回向量的起始和结束迭代器,表示要排序的范围。for (int num : numbers)
:使用范围 for 循环遍历并输出排序后的向量元素。这段代码展示了如何使用 C++ 标准库中的 std::sort
函数对一个整数向量进行排序。
下一篇:c++容器
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站