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

相关推荐
起床气2335 小时前
C++海战棋开发日记(序)
开发语言·c++
APItesterCris5 小时前
TypeScript 与淘宝 API:构建类型安全的商品数据查询前端 / Node.js 服务
开发语言·php
ftpeak5 小时前
《Cargo 参考手册》第二十一章:Cargo 包命令
开发语言·rust
_码力全开_5 小时前
P1005 [NOIP 2007 提高组] 矩阵取数游戏
java·c语言·c++·python·算法·矩阵·go
陈一Tender5 小时前
JavaWeb后端实战(登录认证 & 令牌技术 & 拦截器 & 过滤器)
java·开发语言·spring boot·mysql
墨染点香5 小时前
LeetCode 刷题【124. 二叉树中的最大路径和、125. 验证回文串】
算法·leetcode·职场和发展
Camel卡蒙5 小时前
红黑树详细介绍(五大规则、保持平衡操作、Java实现)
java·开发语言·算法
jerryinwuhan5 小时前
机器人模拟器(python)
开发语言·python·机器人
AhriProGramming5 小时前
Flask-SQLAlchemy精读-双语精选文章
python·算法·flask
孤廖6 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法