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

c++stl库

作者:傲骨依旧   发布日期:2026-01-31   浏览:15

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

// 这个例子展示了如何使用C++ STL库中的vector和algorithm来处理一个简单的任务。

int main() {
    // 创建一个vector容器,存储整数类型的数据
    std::vector<int> numbers = {10, 20, 30, 40, 50};

    // 使用STL算法库中的for_each函数遍历并打印vector中的元素
    std::for_each(numbers.begin(), numbers.end(), [](int n) {
        std::cout << n << " ";
    });
    std::cout << std::endl;

    // 使用STL算法库中的sort函数对vector进行排序
    std::sort(numbers.begin(), numbers.end());

    // 再次遍历并打印排序后的vector中的元素
    std::for_each(numbers.begin(), numbers.end(), [](int n) {
        std::cout << n << " ";
    });
    std::cout << std::endl;

    return 0;
}

解释说明:

  1. 包含头文件#include <iostream> 用于输入输出操作,#include <vector> 用于使用STL中的vector容器,#include <algorithm> 用于使用STL中的算法。
  2. 创建vector容器std::vector<int> numbers = {10, 20, 30, 40, 50}; 创建了一个包含整数的vector,并初始化了一些数据。
  3. 遍历并打印元素:使用std::for_each算法遍历vector中的每个元素,并通过lambda表达式打印出来。
  4. 排序:使用std::sort算法对vector中的元素进行排序。
  5. 再次遍历并打印:排序后再次遍历并打印vector中的元素,展示排序结果。

上一篇:c++运行

下一篇:c++pair

大家都在看

c++闭包

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

c++ 注释

c++如何判断素数

c++格式化字符串

c++ orm框架

c++ random函数用法

队列c++

c++freopen怎么用

c++virtual

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

Laravel 中文站