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

c++排序

作者:我在坚强也需要有人疼   发布日期:2025-08-22   浏览:95

#include <iostream>
#include <vector>
#include <algorithm>

// 示例代码:使用C++标准库中的sort函数对整数向量进行排序

int main() {
    // 创建一个包含整数的向量
    std::vector<int> numbers = {5, 2, 9, 1, 5, 6};

    // 使用std::sort对向量进行排序
    std::sort(numbers.begin(), numbers.end());

    // 输出排序后的结果
    std::cout << "排序后的数组: ";
    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

解释说明:

  1. 包含头文件#include <iostream> 用于输入输出操作,#include <vector> 用于使用向量容器,#include <algorithm> 包含了 std::sort 函数。
  2. 创建向量std::vector<int> numbers = {5, 2, 9, 1, 5, 6}; 创建了一个包含整数的向量。
  3. 调用排序函数std::sort(numbers.begin(), numbers.end()); 使用标准库中的 std::sort 函数对向量进行升序排序。numbers.begin()numbers.end() 分别是向量的起始和结束迭代器。
  4. 输出结果:通过 for 循环遍历并输出排序后的向量元素。

这段代码展示了如何使用 C++ 标准库中的 std::sort 函数对一个整数向量进行排序。

上一篇:c++ <<

下一篇:c++数字转字符串

大家都在看

c++闭包

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

c++ 注释

c++如何判断素数

c++格式化字符串

c++ orm框架

队列c++

c++freopen怎么用

c++ 获取系统时间

c++进制转换函数

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

Laravel 中文站