Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Bind Function
//bind function #include <iostream> #include <functional> #include <numeric> #include <algorithm> #include <list> using namespace std; typedef list<double>DList; typedef DList::iterator DIterator; double cubeX(double x){ return x*x*x; }; int main() { using namespace placeholders; DList list1(14); DIterator it1; iota(list1.begin(), list1.end(), 1.00); cout << "\nlist1:\t[ "; for(auto i : list1) cout << i << " "; cout << "]\n"; DList list2(list1); double x = 3.0; auto cube = bind(cubeX, x); cout << "\ncube(" << x << ") = " << cube() << "\n"; // remove if value is greater than 5 // double val = 5.00; // // The placeholder '_1' and the value 'val' arguments are bound to the comparison // function 'greater<T>()' // it1 = remove_if(list1.begin(), list1.end(), bind(greater<double>(), _1, val)); cout << "\nRemove all values greater than " << val << "\n"; list1.erase(it1, list1.end()); cout << "New list1:\t[ "; for(auto i : list1) cout << i << " "; cout << "]\n"; // remove if value is less than 5 // cout << "\nOld list2:\t[ "; for(auto i : list2) cout << i << " "; cout << "]\n"; val = 5.00; it1 = remove_if(list2.begin(), list2.end(), bind(less<double>(), _1, val)); cout << "\nRemove all values less than " << val << "\n"; list2.erase(it1, list2.end()); cout << "New list2:\t[ "; for(auto i : list2) cout << i << " "; cout << "]\n"; // remove copy if value is less than 8 // DList list3(15); iota(list3.begin(), list3.end(), 1); DList list4(list3); cout << "\nOld list3:\t[ "; for(auto i : list3) cout << i << " "; cout << "]\n"; val = 8.00; DList result(10); cout << "\nRemove all values < " << val << " and copy into \'result\'\n"; it1 = remove_copy_if(list3.begin(), list3.end(), result.begin(), bind(less<double>(), _1, val)); result.erase(it1, result.end()); cout << "result:\t[ "; for(auto i : result) cout << i << " "; cout << "]\n"; // find all values in the range [7, 11) // DIterator ita, itb; ita = find_if(list4.begin(), list4.end(), bind(greater<double>(), _1, 6.5)); itb = find_if(list4.begin(), list4.end(), bind(greater<double>(), _1, 10)); DList list5(ita, itb); cout << "\nFind all values in the range = [" << *ita << ", " << *itb << ")\n"; cout << "\nlist5:\t[ "; for(auto i : list5) cout << i << " "; cout << "]\n"; return 0; }
run
|
edit
|
history
|
help
0
Find Case Combinations of a String
Exempel 1
Dar
maximum_frequent_sum
remove_copy_if-30-Seconds-of-C++
Tree
Stack with min element
Float
UtilityPair2
error