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

目录

[1. 说明](#1. 说明)

[2. 用法示例](#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> 库的一部分。其主要目的是简化使用流进行输入输出操作时对带引号的字符串的处理。

相关推荐
小新学习屋14 分钟前
《剑指offer》-数据结构篇-树
数据结构·算法·leetcode
好心的小明20 分钟前
【深度之眼机器学习笔记】04-01-决策树简介、熵,04-02-条件熵及计算举例,04-03-信息增益、ID3算法
笔记·算法·决策树
go546315846536 分钟前
Python点阵字生成与优化:从基础实现到高级渲染技术
开发语言·人工智能·python·深度学习·分类·数据挖掘
猫头虎40 分钟前
2025年02月11日 Go生态洞察:Go 1.24 发布亮点全面剖析
开发语言·后端·python·golang·go·beego·go1.19
程序员编程指南1 小时前
Qt 网络编程进阶:RESTful API 调用
c语言·网络·c++·qt·restful
仰望天空—永强1 小时前
PS 2025【七月最新v26.5】PS铺软件安装|最新版|附带安装文件|详细安装说明|附PS插件
开发语言·图像处理·python·图形渲染·photoshop
寒士obj1 小时前
JVM 内存结构
java·开发语言·jvm
MediaTea1 小时前
Python 库手册:xmlrpc.client 与 xmlrpc.server 模块
开发语言·python
悦悦子a啊1 小时前
Python之--字典
开发语言·python·学习
程序员编程指南1 小时前
Qt XML 与 JSON 数据处理方法
xml·c语言·c++·qt·json