Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

sort c++

作者:巅峰战犯   发布日期:2026-05-05   浏览:27

#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++右值引用

大家都在看

c++闭包

c++向上取整的代码

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++怎么输入字符串

c++ functional

c++框架代码

c++格式化字符串

c++ orm框架

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站