jack06215

[tutorial] filterNot (filter val from a collection)

Jul 11th, 2020 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 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 filterNot(Collection col,Predicate predicate ) {  
  10.     auto returnIterator = std::remove_if(col.begin(),col.end(),predicate);
  11.     col.erase(returnIterator,std::end(col));    
  12.     return col;
  13. }
  14.  
  15. int main()
  16. {
  17.     auto println = [](const char *message) { std::cout << "\n" << message << '\n'; };
  18.     auto lambda_echo = [](int i) { std::cout << std::setw(3) << i; };
  19.     std::vector<int> col{ 23, 23, 37, 42, 23, 23, 37 };
  20.  
  21.     println("running filterNot");
  22.     auto filterCol = filterNot(col,[](int val){ return val == 37;});
  23.     std::copy(filterCol.begin(), filterCol.end(), std::ostream_iterator<int>(std::cout, " "));
  24.  
  25.     return 0;
  26. }
Add Comment
Please, Sign In to add comment