C++/STL Algorithms Modifying sequence operations/transform — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:28, 25 мая 2010

Compute the difference

  
#include <algorithm>
#include <functional>
#include <iomanip>
#include <vector>
#include <iostream>
using namespace std;
template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main()
{
   const float d1[] = { 1.11, 2.22, 3.33, 4.44, 5.55 };
   const float d2[] = { 6.66, 7.77, 8.88, 9.99, 1.11 };
   vector<float> v2( d2,d2 + sizeof( d2 ) / sizeof( d2[0] ) );
   vector<float> v1( d1,d1 + sizeof( d1 ) / sizeof( d1[0] ) );
   vector<float> change( v2.size() );
   transform( v2.begin(), v2.end(), v1.begin(),change.begin(), minus<float>() );
   print( change );
}


Create a sequence that contains the midpoints between the values in two other sequences.

  
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
double reciprocal(double val);
template<class T> void show(const char *msg, vector<T> vect);
int main(){
  int i;
  vector<double> v;
  for(i=1; i < 10; ++i) 
     v.push_back((double)i);
  show("Initial contents of v:", v);
  cout << endl;
  vector<double> v2(10);
  transform(v.begin(), v.end(), v2.begin(), reciprocal);
  show("v2:", v2);
  return 0;
}
template<class T> void show(const char *msg, vector<T> vect) {
  cout << msg << endl;
  for(unsigned i=0; i < vect.size(); ++i)
    cout << vect[i] << endl;
}
// Return the reciprocal
double reciprocal(double val) {
  if(val == 0.0) 
     return 0.0;
  return 1.0 / val; 
}


Demonstrate both unary and binary function objects.

  
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
// A function object that computes a reciprocal.
class reciprocal : unary_function<double, double> {
public:
  result_type sum;
result_type operator()(argument_type val) {
    if(val == 0.0) 
       return 0.0;
    return 1.0 / val; // return reciprocal
  }
};
int main(){
  int i;
  vector<double> v;
  for(i=1; i < 10; ++i) v.push_back((double)i);
  transform(v.begin(), v.end(), v.begin(), reciprocal());
  return 0;
}


demonstrate the two-sequence form of transform()

  
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int midpoint(int a, int b);
template<class T> void show(const char *msg, vector<T> vect);
int main()
{
  int i;
  vector<double> v;
  for(i=1; i < 10; ++i) 
     v.push_back((double)i);
  show("Initial contents of v:", v);
  vector<int> v3, v4, v5(10);
  for(i = 0; i < 10; ++i) 
     v3.push_back(i);
  for(i = 10; i < 20; ++i) {
     if(i%2) {
        v4.push_back(i); 
     }else {
        v4.push_back(-i);
     }
  }
  show("Contents of v3:", v3);
  show("Contents of v4:", v4);
  transform(v3.begin(), v3.end(), v4.begin(), v5.begin(), midpoint);
  show("Contents of v5:", v5);
  return 0;
}
template<class T> void show(const char *msg, vector<T> vect) {
  cout << msg << endl;
  for(unsigned i=0; i < vect.size(); ++i)
    cout << vect[i] << endl;
}
// Return the whole-number midpoint between two values.
int midpoint(int a, int b) {
  return((a-b) / 2) + b;
}


std::transform with predicate

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
int calculateCube( int );
int main()
{
   std::ostream_iterator< int > output( cout, " " );
   int a2[ 10 ] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
   std::vector< int > v2( a2, a2 + 10 ); // copy of a2
   cout << "Vector v2 contains: ";
   std::copy( v2.begin(), v2.end(), output );
   std::vector< int > cubes( 10 ); // instantiate vector cubes
   // calculate cube of each element in v; place results in cubes
   std::transform( v2.begin(), v2.end(), cubes.begin(), calculateCube );
   cout << "\n\nThe cube of every integer in Vector v is:\n";
   std::copy( cubes.begin(), cubes.end(), output );

   cout << endl;
   return 0;
}
int calculateCube( int value )
{
   return value * value * value;
}
/* 
Vector v2 contains: 100 2 8 1 50 3 8 8 9 10
The cube of every integer in Vector v is:
1000000 8 512 1 125000 27 512 512 729 1000
 */


Subtract the mean from every data point

  
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <vector>
using namespace std;
template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}
int main(){
   const float a[] = { 1, 1.3, 1.5, 0.9, 0.1, 0.2};
   vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) );
   cout << data.size() << " ELEMENTS\n";
   print( data  );
   // compute the mean
   float mean = accumulate( data.begin(), data.end(), 0.0f )/ data.size();
   // subtract the mean from every data point
   vector<float> zero_mean( data );
   transform( zero_mean.begin(), zero_mean.end(), zero_mean.begin(),bind2nd( minus<float>(), mean ) );
   print( zero_mean );
}


Transform all elements into deque from set by multiplying 10

  
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <set>
#include <deque>
#include <algorithm>
using namespace std;
/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;
    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << " ";
    }
    std::cout << std::endl;
}

int main()
{
    set<int,greater<int> > coll1;
    deque<int> coll2;
    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i) {
        coll1.insert(i);
    }
    PRINT_ELEMENTS(coll1,"initialized: ");
    // transform all elements into coll2 by multiplying 10
    transform (coll1.begin(),coll1.end(),        // source
               back_inserter(coll2),             // destination
               bind2nd(multiplies<int>(),10));   // operation
    PRINT_ELEMENTS(coll2,"transformed: ");
}
/* 
initialized: 9 8 7 6 5 4 3 2 1
transformed: 90 80 70 60 50 40 30 20 10
 */


Use a binary function object to find the midpoints between elements in v3 and v4 and store the results in v5.

  
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
// finds the midpoint between two values.
class midpoint : binary_function<int, int, double> {
public:
  result_type operator()(first_argument_type a, second_argument_type b) {
    return((a-b) / 2) + b;
  }
};
int main(){
  int i;
  vector<int> v3, v4, v5(10);
  for(i = 0; i < 10; ++i) 
     v3.push_back(i);
  for(i = 10; i < 20; ++i) 
     if(i%2) 
        v4.push_back(i); 
     else 
        v4.push_back(-i);
  transform(v3.begin(), v3.end(), v4.begin(), v5.begin(), midpoint());
  return 0;
}


Use generic template function with transform

  
 
/* The following code example is taken from the book
 * "C++ Templates - The Complete Guide"
 * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002
 *
 * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
using namespace std;
template <typename T, int VAL>
T addValue (T const& x)
{
    return x + VAL;
}

void call_addValue()
{
    addValue<int,5>(42);
}
template <typename IT, typename OP>
void transform (IT beg, IT end, IT to, OP op)
{
    while (beg != end) {
        *to++ = op(*beg++);
    }
}
int main()
{
    call_addValue();
    int m[] = { 1, 2, 3, 4, 5, 6 };
    transform (m, m+6,
               m,
               (int(*)(int const&))    // cast necessary
               addValue<int,5>);
    for(int i=0;i<6;i++){
       cout << m[i] << " ";
    }
}
/* 
6 7 8 9 10 11
 */


Use transform function to square all elements in an array

  
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
int main()
{
    int coll[] = { 5, 6, 2, 4, 1, 3 };
    // square all elements
    transform (coll, coll+6,        // first source
               coll,                // second source
               coll,                // destination
               multiplies<int>());  // operation
    // print all elements
    copy (coll, coll+6,
          ostream_iterator<int>(cout," "));
    cout << endl;
}
/* 
25 36 4 16 1 9
 */


Use transform to add each element traversed forward with each element traversed backward

  
 
    
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;
    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << " ";
    }
    std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
 * - fill values from first to last into the collection
 * - NOTE: NO half-open range
 */
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
    for (int i=first; i<=last; ++i) {
        coll.insert(coll.end(),i);
    }
}
using namespace std;
int main()
{
    vector<int> coll1;
    list<int> coll2;
    INSERT_ELEMENTS(coll1,1,9);
    PRINT_ELEMENTS(coll1,"coll1:   ");
    /* add each element traversed forward with each element traversed backward
     * and insert result into coll2
     */
    transform (coll1.begin(), coll1.end(),       // first source range
               coll1.rbegin(),                   // second source range
               back_inserter(coll2),             // destination range
               plus<int>());                     // operation
    PRINT_ELEMENTS(coll2,"coll2:   ");
}
/* 
coll1:   1 2 3 4 5 6 7 8 9
coll2:   10 10 10 10 10 10 10 10 10
 */


Use transform to make the numbers in a list go from a certain range

  
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
using namespace std;
int main( )
{
   list<int> data( 100000 );
   // create random numbers
   generate( data.begin(), data.end(), rand );
   // make them go from 0 to 200
   transform( data.begin(), data.end(), data.begin(),bind2nd( modulus<int>(), 201 ) );
   // make them go from -100 to 100
   transform( data.begin(), data.end(), data.begin(),bind2nd( minus<int>(), 100 ) );

}


Use transform to negate all elements in a container

  
 
    
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;
    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << " ";
    }
    std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
 * - fill values from first to last into the collection
 * - NOTE: NO half-open range
 */
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
    for (int i=first; i<=last; ++i) {
        coll.insert(coll.end(),i);
    }
}
using namespace std;
int main()
{
    vector<int> coll1;
    list<int> coll2;
    INSERT_ELEMENTS(coll1,1,9);
    PRINT_ELEMENTS(coll1,"coll1:   ");
    // negate all elements in coll1
    transform (coll1.begin(), coll1.end(),      // source range
               coll1.begin(),                   // destination range
               negate<int>());                  // operation
    PRINT_ELEMENTS(coll1,"negated: ");
}
/* 
coll1:   1 2 3 4 5 6 7 8 9
negated: -1 -2 -3 -4 -5 -6 -7 -8 -9
 */


Use transform to print differences of two corresponding elements

  
 
    
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;
    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << " ";
    }
    std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
 * - fill values from first to last into the collection
 * - NOTE: NO half-open range
 */
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
    for (int i=first; i<=last; ++i) {
        coll.insert(coll.end(),i);
    }
}
using namespace std;
int main()
{
    vector<int> coll1;
    list<int> coll2;
    INSERT_ELEMENTS(coll1,1,9);
    PRINT_ELEMENTS(coll1,"coll1:   ");
    // print differences of two corresponding elements
    cout << "diff:    ";
    transform (coll1.begin(), coll1.end(),       // first source range
               coll2.begin(),                    // second source range
               ostream_iterator<int>(cout, " "), // destination range
               minus<int>());                    // operation
    cout << endl;
}
/* 
coll1:   1 2 3 4 5 6 7 8 9
diff:    2 3 4 5 6 7 8 9 10
 */


Use transform to print elements negatively and in reverse order

  
 
    
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;
    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << " ";
    }
    std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
 * - fill values from first to last into the collection
 * - NOTE: NO half-open range
 */
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
    for (int i=first; i<=last; ++i) {
        coll.insert(coll.end(),i);
    }
}
using namespace std;
int main()
{
    vector<int> coll1;
    list<int> coll2;
    INSERT_ELEMENTS(coll1,1,9);
    PRINT_ELEMENTS(coll1,"coll1:   ");
    // print coll2 negatively and in reverse order
    transform (coll2.rbegin(), coll2.rend(),    // source range
               ostream_iterator<int>(cout," "), // destination range
               negate<int>());                  // operation
    cout << endl;
}
/* 
coll1:   1 2 3 4 5 6 7 8 9

 */


Use transform to square each element

  
 
    
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;
    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << " ";
    }
    std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
 * - fill values from first to last into the collection
 * - NOTE: NO half-open range
 */
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
    for (int i=first; i<=last; ++i) {
        coll.insert(coll.end(),i);
    }
}
using namespace std;
int main()
{
    vector<int> coll1;
    list<int> coll2;
    INSERT_ELEMENTS(coll1,1,9);
    PRINT_ELEMENTS(coll1,"coll1:   ");
    // square each element
    transform (coll1.begin(), coll1.end(),       // first source range
               coll1.begin(),                    // second source range
               coll1.begin(),                    // destination range
               multiplies<int>());               // operation
    PRINT_ELEMENTS(coll1,"squared: ");
}
/* 
coll1:   1 2 3 4 5 6 7 8 9
squared: 1 4 9 16 25 36 49 64 81
 */


Use transform to transform elements in one container into another container with ten times their value

  
 
    
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;
    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << " ";
    }
    std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
 * - fill values from first to last into the collection
 * - NOTE: NO half-open range
 */
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
    for (int i=first; i<=last; ++i) {
        coll.insert(coll.end(),i);
    }
}
using namespace std;
int main()
{
    vector<int> coll1;
    list<int> coll2;
    INSERT_ELEMENTS(coll1,1,9);
    PRINT_ELEMENTS(coll1,"coll1:   ");
    // transform elements of coll1 into coll2 with ten times their value
    transform (coll1.begin(), coll1.end(),      // source range
               back_inserter(coll2),            // destination range
               bind2nd(multiplies<int>(),10));  // operation
    PRINT_ELEMENTS(coll2,"coll2:   ");
}
/* 
coll1:   1 2 3 4 5 6 7 8 9
coll2:   10 20 30 40 50 60 70 80 90
 */