当前位置: 首页 > 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.dtcms.com/a/265942.html

相关文章:

  • STC8G 8051内核单片机开发 (中断)
  • 在 UniApp 项目中巧用开发工具与 AI 插件:全面提升开发到部署的效率
  • 【时间序列数据处理的噩梦与救赎:一次复杂数据可视化问题的深度复盘】
  • 运维服务部初级服务工程师面招聘笔试题和答案
  • PROFINET转MODBUS TCP网关在机械臂通信操作中的应用研究
  • 微信小程序——skyline版本问题
  • 2025年金融创新与计算机视觉国际会议(FICV 2025)
  • 【网络协议】WebSocket简介
  • Web 服务器架构选择深度解析
  • HTTP-Postman的安装及其使用
  • 电脑CPU使用率占用100%怎么办 解决步骤指南
  • 【数字后端】- 衡量design的congestion情况
  • HTTP各版本变化详解
  • C# 线程同步(一)同步概念介绍
  • 基于Anything LLM的本地知识库系统远程访问实现路径
  • react-打包和本地预览 ——打包优化
  • 基于CNN的人脸关键点检测
  • 强实时运动控制内核MotionRT750(一):驱动安装、内核配置与使用
  • 【科普】Cygwin与wsl与ssh连接ubuntu有什么区别?DIY机器人工房
  • 【大模型学习】项目练习:文档对话助手
  • 零碳园区如何建设,微电网系统来助力
  • 离线迁移 Conda 环境到 Windows 服务器:用 conda-pack 摆脱硬路径限制
  • 拉横幅横幅识别检测数据集VOC+YOLO格式1962张1类别
  • WAIC 2025预告 | 网易灵动发布+展览,两大「全球首发」即将亮相
  • AS32S601 芯片在卫星互联网推进系统中的技术适配性研究
  • Linux下基于C++11的socket网络编程(Epoll)个人总结版
  • ssh挂载拷贝
  • Java 大视界 -- Java 大数据机器学习模型在自然语言处理中的跨语言信息检索与知识融合(331)
  • 汽车ECU产线烧录和检测软件怎么做?
  • 云计算中的tap口、bond口、qr口:它们究竟有何玄机?