Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib transform
Personal tools
Document Actions

transform



Click on the banner to return to the class reference home page.

transform


Algorithm

Summary

Applies an operation to a range of values in a collection and stores the result.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

#include <algorithm>

template <class InputIterator,
          class OutputIterator,
          class UnaryOperation>
OutputIterator transform (InputIterator first,
                          InputIterator last,
                          OutputIterator result,
                          UnaryOperation op);

template <class InputIterator1,
          class InputIterator2,
          class OutputIterator,
          class BinaryOperation>
OutputIterator transform (InputIterator1 first1,
                          InputIterator1 last1,
                          InputIterator2 first2,
                          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 [first, last), and sends the result to 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 [first1, last1) and the range that begins at first2, and sends the result to 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 + (last1 - first1)) a new corresponding value equal to:

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

or

binary_op(*(first1 + (i - result), *(first2 + (i - result)))

transform returns result + (last1 - first1). op and binary_op must not have any side effects. result may be equal to first in case of unary transform, or to first1 or first2 in case of binary transform.

Complexity

Exactly last1 - first1 applications of op or binary_op are performed.

Example

//
// trnsform.cpp
//
 #include <functional>
 #include <deque>
 #include <algorithm>
 #include <iostream.h>
 #include <iomanip.h>
 int main()
 {
   //Initialize a deque with an array of ints
   int arr1[5] = {99, 264, 126, 330, 132};
   int arr2[5] = {280, 105, 220, 84, 210};
   deque<int> d1(arr1, arr1+5), d2(arr2, arr2+5);
   //Print the original values
   cout << "The following pairs of numbers: " 
        << endl << "     ";
   deque<int>::iterator i1;
   for(i1 = d1.begin(); i1 != d1.end(); i1++)
      cout << setw(6) << *i1 << " ";
   cout << endl << "     ";
   for(i1 = d2.begin(); i1 != d2.end(); i1++)
      cout << setw(6) << *i1 << " ";
   // Transform the numbers in the deque to their 
   // factorials and store in the vector
   transform(d1.begin(), d1.end(), d2.begin(), 
             d1.begin(), multiplies<int>());
   //Display the results
   cout << endl << endl;
   cout << "Have the products: " << endl << "     ";
   for(i1 = d1.begin(); i1 != d1.end(); i1++)
     cout << setw(6) << *i1 << " ";
   return 0;
 }

Output :
The following pairs of numbers:
         99    264    126    330    132
        280    105    220     84    210
Have the products:
      27720  27720  27720  27720  27720

Warning

If your compiler does not support default template parameters, then you need to always supply the Allocator template argument. For instance, you will need to write :

deque<int, allocator<int> >

instead of:

deque<int>


©Copyright 1996, Rogue Wave Software, Inc.

Weather
No information available
 

Powered by Plone This site conforms to the following standards: