jack06215

[tutorial] if_exist (check if val exists in collection)

Jul 11th, 2020 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 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 Condition>
  9. bool if_exist(Collection col, Condition con) {
  10.     auto exist = std::find_if(col.begin(), col.end(), con);
  11.     return exist != col.end();
  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 exist() function");
  20.     auto check_func = [] (int val) {
  21.         return val == 20;
  22.     };
  23.     std::cout << if_exist(col, check_func)  << '\n';
  24.  
  25.     return 0;
  26. }
Add Comment
Please, Sign In to add comment