首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::transform

Defined in header <algorithm>

?

?

template< class InputIt, class OutputIt, class UnaryOperation > OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryOperation > ForwardIt2 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 d_first, UnaryOperation unary_op );

(2)

(since C++17)

template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation > OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op );

(3)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3, class BinaryOperation > ForwardIt3 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt3 d_first, BinaryOperation binary_op );

(4)

(since C++17)

std::transform将给定的函数应用于范围,并将结果存储在另一个范围中,从d_first...

1%29一年期手术unary_op应用于[first1, last1)...

3%29二进制操作binary_op应用于来自两个范围的元素对:一个由[first1, last1)而另一个开始于first2...

2,4%29与%281,3%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

unary_op and binary_op must not have side effects.

(until C++11)

unary_op and binary_op must not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved.

(since C++11)

参数

first1, last1

-

the first range of elements to transform

first2

-

the beginning of the second range of elements to transform

d_first

-

the beginning of the destination range, may be equal to first1 or first2

policy

-

the execution policy to use. See execution policy for details.

unary_op

-

unary operation function object that will be applied. The signature of the function should be equivalent to the following: Ret fun(const Type &a); The signature does not need to have const &. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. ?

binary_op

-

binary operation function object that will be applied. The signature of the function should be equivalent to the following: Ret fun(const Type1 &a, const Type2 &b); The signature does not need to have const &. The types Type1 and Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. ?

类型要求

-InputIt、InputIt 1、InputIt 2必须符合InputIterator的要求。

-输出必须符合输出器的要求。

-前进It 1、前进It 2、前进It 3必须符合先行者的要求。

返回值

将迭代器输出到经过最后一个转换的元素。

复杂性

1-2%29std::distance(first1, last1)的应用unary_op

3-4%29std::distance(first1, last1)的应用binary_op

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<类Inputit,类Outputit,类UnaryOperation>OutputIt转换%28 Inputitfirst 1,InputitList 1,OutputIt d[医]第一,一战一战[医]OP%29{而%28first 1%21=last1%29{%2A丁[医]第一++=一元[医]执行部分第28段%2A首先1++%29;}返回d[医]第一;}

第二版

模板<类InputIt 1,类InputIt 2,类OutputIt,类BinaryOperating>OutputIt转换%28InputIt1first 1,InputIt1last1,InputIt2first 2,Outputit d[医]首先,二进制运算[医]OP%29{而%28first 1%21=last1%29{%2A丁[医]第一++=二进制[医]执行部分第28段%2A先1++,%2A前2++%29;}返回d[医]第一;}

注记

std::transform不保证按顺序适用unary_opbinary_op若要按顺序将函数应用于序列或应用修改序列元素的函数,请使用std::for_each...

下面的代码使用Transform将字符串转换为大写,使用Touper函数:

二次

代码语言:javascript
复制
#include <string>
#include <cctype>
#include <algorithm>
#include <iostream>
 
int main()
{
    std::string s("hello");
    std::transform(s.begin(), s.end(), s.begin(),
                   [](unsigned char c) { return std::toupper(c); });
    std::cout << s;
}

二次

产出:

二次

代码语言:javascript
复制
HELLO

二次

另见

for_each

applies a function to a range of elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com