jack06215

[tutorial] filter (extract list of vals on a condition)

Jul 11th, 2020 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <functional>
  6. #include <iterator>
  7.  
  8. template <typename Collection,typename Predicate>
  9. Collection filter(Collection col, Predicate predicate) {
  10.     auto fnCol = filterNot(col,[predicate](typename Collection::value_type i) { return !predicate(i);});
  11.     return fnCol;
  12. }
  13.  
  14. int main()
  15. {
  16.     auto println = [](const char *message) { std::cout << "\n" << message << '\n'; };
  17.     std::vector<int> col{ 23, 23, 37, 42, 23, 23, 37 };
  18.  
  19.     println("running filter");
  20.     auto condition = [](int value) {
  21.         return value > 30;
  22.     };
  23.     auto filterCol = filter(col,condition);
  24.     std::copy(filterCol.begin(), filterCol.end(), std::ostream_iterator<int>(std::cout, " "));
  25.  
  26.     return 0;
  27. }
Add Comment
Please, Sign In to add comment