Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library Reference Guide

transform()

Library:  Algorithms


Function

Local Index

No Entries

Summary

Algorithm that applies an operation to a range of values in a collection and stores the result

Synopsis

#include <algorithm>

namespace std {
  template <class InputIterator, class OutputIterator,
            class UnaryOperation>
  OutputIterator 
  transform(InputIterator start, InputIterator finish,
            OutputIterator result, UnaryOperation op);

  template <class InputIterator1, class InputIterator2,
            class OutputIterator, class BinaryOperation>
  OutputIterator 
  transform(InputIterator1 start1, InputIterator1 finish1,
            InputIterator2 start2, OutputIterator result,
            BinaryOperation binary_op);
}

Description

The transform() algorithm has two forms. The first form applies unary operation op to each element of the range [start, finish), and assigns the result to the element pointed to by the output iterator result. For example, this version of transform() could be used to square each element in a vector. If the output iterator (result) is the same as the input iterator used to traverse the range, transform() performs its transformation in place.

The second form of transform() applies a binary operation, binary_op, to corresponding elements in the range [start1, finish1) and the range that begins at start2, and assigns the result to the element pointed to by result. For example, transform() can be used to add corresponding elements in two sequences, and store the set of sums in a third. The algorithm assumes, but does not check, that the second sequence has at least as many elements as the first sequence. Note that the output iterator result can be a third sequence, or either of the two input sequences.

Formally, transform() assigns through every iterator i in the range [result, result + (finish1 - start1)) a new corresponding value equal  to:

op(*(start1 + (i - result))

or:

binary_op(*(start1 + (i - result), *(start2 + (i - result)))

transform() returns result + (finish1 - start1). op and binary_op must not have any side effects. result may be equal to start in case of unary transform, or to start1 or start2 in case of binary transform.

Complexity

Exactly finish1 - start1 applications of op or binary_op are performed.

Example

Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.2.3



Previous fileTop of DocumentContentsIndex pageNext file