#include <iostream>
#include <vector>
#include <algorithm>
int main() {
// 创建一个整数向量
std::vector<int> numbers = {5, 2, 8, 1, 9, 3};
// 使用 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>:引入了 C++ 标准库中的算法头文件,其中包含 std::sort 函数。std::vector<int> numbers = {5, 2, 8, 1, 9, 3};:创建一个包含整数的向量,并初始化了一些值。std::sort(numbers.begin(), numbers.end());:调用 std::sort 函数对向量中的元素进行升序排序。numbers.begin() 和 numbers.end() 分别表示向量的起始和结束迭代器。for (int num : numbers):使用范围基于的 for 循环遍历并输出排序后的向量元素。这段代码展示了如何使用 C++ 中的 std::sort 函数对一个整数向量进行排序。
上一篇:c++ getline函数用法
下一篇:c++右值引用
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站