当前位置: 首页 > news >正文

C++ 标准模板库算法之 transform 用法

目录

1. 说明

2. 用法示例


1. 说明

std::transform 是一种多功能算法,用于将已知函数应用于一个或多个范围内的元素并将结果存储在输出范围内。它主要有两种形式:一元运算和二元运算。具体来说是在 <algorithm> 标头中。函数声明:

template< class InputIt, class OutputIt, class UnaryOp >

OutputIt transform( InputIt first1, InputIt last1,

                    OutputIt d_first, UnaryOp unary_op );

(1)

(constexpr since C++20)

template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class UnaryOp >
ForwardIt2 transform( ExecutionPolicy&& policy,
                      ForwardIt1 first1, ForwardIt1 last1,

                      ForwardIt2 d_first, UnaryOp unary_op );

(2)

(since C++17)

template< class InputIt1, class InputIt2,

          class OutputIt, class BinaryOp >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2,

                    OutputIt d_first, BinaryOp binary_op );

(3)

(constexpr since C++20)

template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2,
          
class ForwardIt3, class BinaryOp >
ForwardIt3 transform( ExecutionPolicy&& policy,
                      ForwardIt1 first1, ForwardIt1 last1,
                      ForwardIt2 first2,

                      ForwardIt3 d_first, BinaryOp binary_op );

(4)

(since C++17)

一元操作的函数相当于:Ret fun(const Type &a);

二元操作的函数相当于:Ret fun(const Type1 &a, const Type2 &b);

即传进去的是一个函数对象。

2. 用法示例

#include <algorithm>

#include <cctype>

#include <iomanip>

#include <iostream>

#include <string>

#include <utility>

#include <vector>

 

void print_ordinals(const std::vector<unsigned>& ordinals)

{

    std::cout << "ordinals: ";

    for (unsigned ord : ordinals)

        std::cout << std::setw(3) << ord << ' ';

    std::cout << '\n';

}

 

char to_uppercase(unsigned char c)

{

    return std::toupper(c);

}

 

void to_uppercase_inplace(char& c)

{

    c = to_uppercase(c);

}

 

void unary_transform_example(std::string& hello, std::string world)

{

    // string就地转化为大写形式

 

    std::transform(hello.cbegin(), hello.cend(), hello.begin(), to_uppercase);

    std::cout << "hello = " << std::quoted(hello) << '\n';

 

    // for_each version (see Notes above)

    std::for_each(world.begin(), world.end(), to_uppercase_inplace);

    std::cout << "world = " << std::quoted(world) << '\n';

}

 

void binary_transform_example(std::vector<unsigned> ordinals)

{

    // 将数转换为double

 

    print_ordinals(ordinals);

 

    std::transform(ordinals.cbegin(), ordinals.cend(), ordinals.cbegin(),

                   ordinals.begin(), std::plus<>{});

 //或使用 std::plus<>()

    print_ordinals(ordinals);

}

 

int main()

{

    std::string hello("hello");

    unary_transform_example(hello, "world");

 

    std::vector<unsigned> ordinals;

    std::copy(hello.cbegin(), hello.cend(), std::back_inserter(ordinals));

    binary_transform_example(std::move(ordinals));

}

输出:

hello = "HELLO"

world = "WORLD"

ordinals:  72  69  76  76  79

ordinals: 144 138 152 152 158

说明:std::quoted 是 C++14 中引入的 I/O 操作符,属于 <iomanip> 库的一部分。其主要目的是简化使用流进行输入输出操作时对带引号的字符串的处理。

http://www.lryc.cn/news/579444.html

相关文章:

  • RAG从入门到高阶(二):Retrieve-and-Rerank
  • 开源无广告面板mdserver-web:替代宝塔实现服务器轻松管理
  • NCCL的基本使用和常用通信算法源码分析
  • 洛谷-循环结构(1)
  • 前端框架中注释占位与Fragment内容替换的实现与优化
  • 网络基础(3)
  • Spring 6 源码深度掘金:66+核心原理与高频面试攻坚指南
  • 【科研绘图系列】基于R语言的种质资源评分相关性分析与可视化教程
  • 【零基础学AI】第21讲:TensorFlow基础 - 神经网络搭建入门
  • 从生活实例看:点积、内积和矩阵乘法如何玩转机器学习
  • 【maven仓库搜索下载工作流程】
  • 后端 Maven打包 JAR 文件、前端打包dist文件、通过后端服务访问前端页面、Nginx安装与部署
  • 办公文档批量打印器 Word、PPT、Excel、PDF、图片和文本,它都支持批量打印。
  • Flask 遇到了 AttributeError: ‘Babel‘ object has no attribute ‘localeselector‘ 怎么解决
  • TinyWebserver学习(8)-定时器
  • 在 Jetson Orin 开发套件上使用 Hardware Encoder / Decoder 构建 FFmpeg
  • 仿真软件介绍 COMSOL Multiphysics 或 ANSYS Fluent 等 MATLAB OpenFOAM,和在化学上的应用实例
  • 2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
  • Springboot3整合ehcache3缓存--XML配置和编程式配置
  • 【PyCharm 2025.1.2配置debug】
  • 【vmware虚拟机使用】 开始安装centos7操作系统
  • Navicat Premium 12连接Oracle时提示oracle library is not loaded的问题解决
  • 分布式部署下如何做接口防抖---使用分布式锁
  • macOS 26正式发布,全新Liquid Glass设计语言亮相
  • 旅游管理实训室:支撑实践教学的核心载体
  • 5118 API智能处理采集数据教程
  • 项目——视频共享系统测试
  • 【C++】状态模式
  • GitHub 解码指南:用 AI 赋能,五步快速掌握任意开源项目
  • MySQL 8.0 OCP 1Z0-908 题目解析(20)