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

相关推荐
m0_748248021 天前
C++20 协程:在 AI 推理引擎中的深度应用
java·c++·人工智能·c++20
笑我归无处1 天前
强引用、软引用、弱引用、虚引用详解
java·开发语言·jvm
02苏_1 天前
秋招Java面
java·开发语言
ytttr8731 天前
64QAM信号的数字预失真处理(MATLAB实现)
开发语言·matlab
他们叫我一代大侠1 天前
Leetcode :模拟足球赛小组各种比分的出线状况
算法·leetcode·职场和发展
Nebula_g1 天前
C语言应用实例:硕鼠游戏,田忌赛马,搬桌子,活动选择(贪心算法)
c语言·开发语言·学习·算法·游戏·贪心算法·初学者
爱吃甜品的糯米团子1 天前
详解 JavaScript 内置对象与包装类型:方法、案例与实战
java·开发语言·javascript
QT 小鲜肉1 天前
【Git、GitHub、Gitee】按功能分类汇总Git常用命令详解(超详细)
c语言·网络·c++·git·qt·gitee·github
派大星爱吃猫1 天前
C++中的inline函数(内联函数)
c++·inline·内联函数
清风wxy1 天前
Duilib_CEF桌面软件实战之Duilib编译与第一个界面程序
c++·笔记·ui·mfc