std::transform C++

本文详细介绍了C++标准库中的std::transform函数用法,包括一元操作和二元操作两种形式,并通过实例展示了如何使用该函数进行数据转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


std::transform

<algorithm>
unary operation(1)
template <class InputIterator, class OutputIterator, class UnaryOperation>
  OutputIterator transform (InputIterator first1, InputIterator last1,
                            OutputIterator result, UnaryOperation op);
binary operation(2)
template <class InputIterator1, class InputIterator2,
          class OutputIterator, class BinaryOperation>
  OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
                             InputIterator2 first2, OutputIterator result,
                             BinaryOperation binary_op );
Transform range
Applies an operation sequentially to the elements of one (1) or two (2) ranges and stores the result in the range that begins at  result.

(1) unary operation
Applies  op to each of the elements in the range  [first1,last1) and stores the value returned by each operation in the range that begins at  result.
(2) binary operation
Calls  binary_op using each of the elements in the range  [first1,last1) as first argument, and the respective argument in the range that begins at  first2 as second argument. The value returned by each call is stored in the range that begins at  result.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
template <class InputIterator, class OutputIterator, class UnaryOperator>
  OutputIterator transform (InputIterator first1, InputIterator last1,
                            OutputIterator result, UnaryOperator op)
{
  while (first1 != last1) {
    *result = op(*first1);  // or: *result=binary_op(*first1,*first2++);
    ++result; ++first1;
  }
  return result;
}


The function allows for the destination range to be the same as one of the input ranges to make transformations in place.

Parameters

first1, last1
Input iterators to the initial and final positions of the first sequence. The range used is  [first1,last1), which contains all the elements between  first1 and  last1, including the element pointed by  first1 but not the element pointed by  last1.
first2
Input iterator to the initial position of the second range. The range includes as many elements as [first1,last1).
result
Output iterator to the initial position of the range where the operation results are stored. The range includes as many elements as  [first1,last1).
op
Unary function that accepts one element of the type pointed by  InputIterator as argument, and returns some result value convertible to the type pointed by  OutputIterator.
This can either be a function pointer or a function object.
binary_op
Binary function that accepts two elements as argument (one of each of the two sequences), and returns some result value convertible to the type pointed by  OutputIterator.
This can either be a function pointer or a function object.

Neither  op nor  binary_op should directly modify the elements passed as its arguments: These are indirectly modified by the algorithm (using the return value) if the same range is specified for  result.

Return value

An iterator pointing to the element that follows the last element written in the  result sequence.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// transform algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::transform
#include <vector>       // std::vector
#include <functional>   // std::plus

int op_increase (int i) { return ++i; }

int main () {
  std::vector<int> foo;
  std::vector<int> bar;

  // set some values:
  for (int i=1; i<6; i++)
    foo.push_back (i*10);                         // foo: 10 20 30 40 50

  bar.resize(foo.size());                         // allocate space

  std::transform (foo.begin(), foo.end(), bar.begin(), op_increase);
                                                  // bar: 11 21 31 41 51

  // std::plus adds together its two arguments:
  std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
                                                  // foo: 21 41 61 81 101

  std::cout << "foo contains:";
  for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


Output:
first contains: 21 41 61 81 101

### C++ 中 `std::transform` 的用法 `std::transform` 是 C++ 标准库中的一个重要算法,位于头文件 `<algorithm>` 中。此函数用于将某个范围内的元素通过指定的操作转换并存储到另一个范围内。 #### 单一输入序列变换 当处理单一输入序列时,`std::transform` 可以接受两个迭代器定义的源区间以及目标区间的起始位置,再加上一个用于描述如何转换每个元素的一元操作。下面的例子展示了如何利用 lambda 表达式来创建一个新的整数向量,其值为原向量各元素平方后的结果[^1]: ```cpp #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> original = {1, 2, 3, 4, 5}; std::vector<int> squared(original.size()); std::transform( original.begin(), original.end(), squared.begin(), [](const int& value) -> int { return value * value; } ); for(auto val : squared){ std::cout << val << " "; } return 0; } ``` 这段代码会输出:`1 4 9 16 25` #### 多重输入序列变换 对于涉及多个输入序列的情况,则需要提供四个迭代器参数——前三个分别指向第一个和第二个源区间的起点及终点,最后一个是指定的目标区域开头;此外还需要二元运算符说明怎样组合来自不同集合的数据项。这里给出一段示范程序,它计算两数组对应位置数值之积,并存入第三个容器内: ```cpp #include <iostream> #include <vector> #include <algorithm> int main(){ std::vector<int> vecA = {1, 2, 3}, vecB = {4, 5, 6}; std::vector<int> product(vecA.size()); std::transform( vecA.begin(), vecA.end(), vecB.begin(), product.begin(), std::multiplies<int>() ); for(int i : product){ std::cout << i << ' '; } return 0; } ``` 上述实例将会打印出:`4 10 18`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值