Mega Code Archive

 
Categories / C++ / Algorithm
 

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 ) ); }