I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com

transform

Modifies (and copies) elements; combines elements of two ranges
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The transform() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class InputIterator, class OutputIterator, class UnaryFunction >
   OutputIterator transform(
      InputIterator first1, 
      InputIterator last1, 
      OutputIterator result,
      UnaryFunction f
   );
template < class InputIterator1, class InputIterator2, class OutputIterator,
   class BinaryFunction >
   OutputIterator transform(
      InputIterator1 first1, 
      InputIterator1 last1,
      InputIterator2 first2, 
      OutputIterator result,
      BinaryFunction f
   );

Parameters:
Parameter Description
first1 An input iterator addressing the position of the first element in the first source range to be operated on
last1 An input iterator addressing the position one past the final element in the first source range operated on
first2 An input iterator addressing the position of the first element in the second source range to be operated on
result An output iterator addressing the position of the first element in the destination range
f User-defined unary function object used in the first version of the algorithm that is applied to each element in the first source range OR a user-defined binary function object used in the second version of the algorithm that is applied pairwise, in a forward order, to the two source ranges

Description

The first form of transform change the range [first1,last1) into a range beginning at result by applying a unary function f.

The second form perform in a manner analogous to the previous one, except that in this case the binary function f is used instead of a unary function. This binary function takes the current element of the first range [first1,last1) as its first parameter, and the corresponding element from the second range as its second parameter.

Return Value

The return value is an iterator pointing to the end of the transformed range.

Complexity

The complexity is linear. It performs last1 - first1 operations.
Example:
Example - transform algorithm
Problem
This program illustrates the functionality of both versions of transform() algorithm.
Workings
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
 
using namespace std;
 
// the function object multiplies an element by a Factor
template <class Type>
class MultValue
{
  private:
    Type Factor; // the value to multiply by
  public:
    // constructor initializes the value to multiply by
    MultValue(const Type& _Val) : Factor(_Val) {    } 
 
    // the function call for the element to be multiplied
    int operator()(Type& elem) const
      {return (elem * Factor);}
};
 
int main()
{
  vector <int> vec1, vec2(7), vec3(7);
  vector <int>::iterator Iter1, Iter2, Iter3;
 
  // constructing vector vec1
  for (int i = -4; i <= 2; i++)
    vec1.push_back(i);
  cout <<"Original vector vec1 data: ";
  for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
 
  // modifying the vector vec1 in place
  transform(vec1.begin(), vec1.end(), vec1.begin(),
                                      MultValue<int>(2));
  cout <<"\nThe elements of the vector vec1 multiplied by 2 in place gives:"
         "\nvec1mod data: ";
  for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
 
  // using transform() to multiply each element by a factor of 5
  transform(vec1.begin(), vec1.end(), vec2.begin(),
                                      MultValue<int>(5));
  cout <<"\nMultiplying the elements of the vector vec1mod"
         "\nby the factor 5 & copying to vec2 gives:\nvec2 data: ";
  for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
    cout <<*Iter2<<" ";
  cout <<endl;
 
  // the second version of transform used to multiply the elements of the vectors vec1mod & vec2 pairwise
  transform(vec1.begin(), vec1.end(), vec2.begin(), vec3.begin(), 
                                                    multiplies<int>());
  cout <<"\nMultiplying elements of the vectors vec1mod and" 
         "vec2 pairwise gives:\nvec3 data: ";
  for (Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++)
    cout <<*Iter3<<" ";
  cout <<endl;
 
  return 0;
}
Solution
Output:

Original vector vec1 data:
-4 -3 -2 -1 0 1 2

The elements of the vector vec1 multiplied by 2 in place gives:
vec1mod data: -8 -6 -4 -2 0 2 4

Multiplying the elements of the vector vec1mod
by the factor 5 & copying to vec2 gives:
vec2 data: -40 -30 -20 0 10 20

Multiplying elements of the vectors vec1mod and vec2 pairwise gives:
vec3 data: 320 180 80 20 0 20 80
References