C++/STL Algorithms Helper/divides
Содержание
Divided contents of a list with transform() and divides()
#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
list<double> l;
list<double> divisors;
int i;
for(i=10; i<100; i+=10) l.push_back((double)i);
for(i=1; i<10; i++) divisors.push_back(3.0);
list<double>::iterator p = l.begin();
while(p != l.end()) {
cout << *p << endl;
p++;
}
p = transform(l.begin(), l.end(),divisors.begin(), l.begin(),divides<double>()); // call function object
p = l.begin();
while(p != l.end()) {
cout << *p << " ";
p++;
}
return 0;
}
Transform a vector with bind1st and divides
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <string>
#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;
}
}
bool in_string( char c, const string target ){
return target.find( c ) != string::npos;
}
int main( ){
vector<float> v( 5, 1 );
partial_sum( v.begin(), v.end(), v.begin() );
print( v);
transform( v.begin(), v.end(), v.begin(),bind1st( divides<float>(), 1 ) );
cout << fixed << setprecision( 2 );
print( v);
// convert to percentages
transform( v.begin(), v.end(), v.begin(),bind1st( multiplies<float>(), 100 ) );
print( v);
// make a sequence starting at -10 and increasing by 100
v.assign( v.size(), 100 );
v[0] = -10;
partial_sum( v.begin(), v.end(), v.begin() );
print( v );
// truncate numbers to fall between 0 and 255 inclusive
replace_if( v.begin(), v.end(),bind2nd( greater<float>(), 255 ), 255 );
replace_if( v.begin(), v.end(), bind2nd( less<float>(), 0 ), 0 );
print( v);
}
Transform with divides
#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( change.begin(), change.end(), v1.begin(),change.begin(), divides<float>() );
print( change );
}
Use a binary function object divides
#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
list<double> vals;
list<double> divisors;
for(int i=10; i<100; i+=10) vals.push_back((double)i);
for(int i=1; i<10; i++) divisors.push_back(3.0);
cout << "Original contents of vals:\n";
list<double>::iterator p = vals.begin();
while(p != vals.end()) {
cout << *p << " ";
p++;
}
cout << endl;
p = transform(vals.begin(), vals.end(),
divisors.begin(), vals.begin(),
divides<double>()); // call function object
cout << "Divided contents of vals:\n";
p = vals.begin();
while(p != vals.end()) {
cout << *p << " ";
p++;
}
return 0;
}
/*
Original contents of vals:
10 20 30 40 50 60 70 80 90
Divided contents of vals:
3.33333 6.66667 10 13.3333 16.6667 20 23.3333 26.6667 30
*/