transform, bind2nd and plus : bind2nd « STL Algorithms Helper « C++






transform, bind2nd and plus

  
#include <algorithm>
#include <functional>
#include <cstdlib>
#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( )
{
   vector<int> random( 8 );

   generate( random.begin(), random.end(), rand );
   print( random);

   transform( random.begin(), random.end(), random.begin(),bind2nd( plus<int>(), 1 ) );
   print( random );
}
  
    
  








Related examples in the same category

1.Demonstrate bind2nd()Demonstrate bind2nd()
2.Use the generic count algorithm with predicate: Determine the number of array elements that are not equal to 1
3.transform, bind2nd and modulus
4.multiply by 100 to get a percent
5.Count vowels in a sentence