Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Exploring the Transform Function and Operator Classes
//Using transform function #include <iostream> #include <iomanip> #include <algorithm> #include <numeric> #include <iterator> #include <vector> #include <set> using namespace std; typedef vector<double>DVect; typedef DVect::iterator dIterator; typedef set<double, less<double> >DSet; typedef DSet::iterator sIterator; ostream& operator <<(ostream& out, const DVect& v){ copy(v.begin(), v.end(), ostream_iterator<double>(out, " ")); return out; }; int main() { int sz = 11; DVect fv(sz); DVect rv; DVect out1(sz); iota(fv.begin(), fv.end(), 1); double data1[11] = {1, 1.50, 3, 5, 7, 9, 11, 13, 14, 16, 12}; copy(data1, data1 + sz, inserter(rv, rv.begin())); cout.setf(ios::showpoint) << cout.precision(3); cout << "\nfv:\t[ "; for(auto i : fv) cout << i << " "; cout << "]"; // // reverse it // cout << "\npv:\t[ "; for(auto i : rv) cout << i << " "; cout << "]\n\n"; // add corresponding elements of fv and rv sequences // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), plus<double>()); cout << "fv + pv:\t[ "; for(auto i:out1) cout << i << " "; cout << "]\n"; // substract corresponding elements of fv and rv sequences // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), minus<double>()); cout << "fv - pv:\t[ "; for(auto i:out1) cout << i << " "; cout << "]\n"; // multiply corresponding elements of fv and rv sequences // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), multiplies<double>()); cout << "fv * pv:\t[ "; for(auto i:out1) cout << i << " "; cout << "]\n"; // divide corresponding elements of fv and rv sequences // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), divides<double>()); cout << "fv / pv:\t[ "; cout.setf(ios::showpoint) << cout.precision(3); for(auto i:out1) cout << i << " "; cout << "]\n"; // modulus of corresponding elements of fv and rv sequences // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), modulus<int>()); cout << "fv % pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n\n"; // equal_to fv == rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), equal_to<double>()); cout << "fv == pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // greater_equal fv >= rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), greater_equal<double>()); cout << "fv >= pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // less_equal fv <= rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), less_equal<double>()); cout << "fv <= pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // less fv < rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), less<double>()); cout << "fv < pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // greater fv > rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), greater<double>()); cout << "fv > pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n\n"; // logical_and fv && rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), logical_and<double>()); cout << "fv && pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // logical_or fv || rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), logical_or<double>()); cout << "fv || pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // logical_not !fv // transform(fv.begin(), fv.end(), out1.begin(), logical_not<double>()); cout << "!fv:\t\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n\n"; // bit_or fv | rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), bit_or<bool>()); cout << "fv | pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // bit_xor fv ^ rv // transform(fv.begin(), fv.end(), rv.begin(), out1.begin(), bit_xor<bool>()); cout << "fv ^ pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out1) cout << i << " "; cout << "]\n"; // bit_and fv & rv // DVect out2(sz); transform(fv.begin(), fv.end(), rv.begin(), out2.begin(), bit_and<bool>()); cout << "fv & pv:\t[ "; cout.unsetf(ios::showpoint); for(auto i:out2) cout << i << " "; cout << "]\n"; DVect b1{1, 1, 1, 0, 1, 0, 0, 0, 1}; DVect b2{1, 0, 0, 1, 1, 1, 0, 2, 3}; DVect out3(b1.size()); cout << "\nb1:\t\t["; for(auto i : b1) cout << i << " "; cout << "]"; cout << "\nb2:\t\t["; for(auto i : b2) cout << i << " "; cout << "]\n"; transform(b1.begin(), b1.end(), b2.begin(), out3.begin(), bit_and<bool>()); cout << "\nb1 & b2:\t["; for(auto i : out3) cout << i << " "; cout << "]"; transform(b1.begin(), b1.end(), b2.begin(), out3.begin(), bit_or<bool>()); cout << "\nb1 | b2:\t["; for(auto i : out3) cout << i << " "; cout << "]"; transform(b1.begin(), b1.end(), b2.begin(), out3.begin(), bit_xor<bool>()); cout << "\nb1 ^ b2:\t["; for(auto i : out3) cout << i << " "; cout << "]\n"; }
run
|
edit
|
history
|
help
0
adjacent_difference-30-Seconds-Of-CPP
all possible palindrome partitions
My billing system
c++ car racing game
Hello
Count squares
CPP - Ex 5
Memory_test
Microsoft - MaxEmployeeAttendence (R repititions - Semi Optimised DP)
TmpFib