fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <initializer_list>
  5.  
  6. struct flag {
  7. bool set = false;
  8. template <class T> void operator=(const T&) { set = true; }
  9. flag& operator++() { return *this; }
  10. flag& operator*() { return *this; }
  11. };
  12.  
  13. template <class CLeft, class CRight>
  14. bool intersects(const CLeft& lhs, const CRight& rhs) {
  15. flag f;
  16. return std::set_intersection(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), f).set;
  17. }
  18.  
  19. bool check(const std::vector<int>& lhs, const std::vector<int>& rhs) {
  20. return intersects(lhs, rhs);
  21. }
  22.  
  23. int main() {
  24. std::cout << std::boolalpha;
  25. std::cout << "{1, 2} ∩ {3, 4} ? " << check({1, 2}, {3, 4}) << "\n";
  26. std::cout << "{1, 2} ∩ {0, 2} ? " << check({1, 2}, {0, 2}) << "\n";
  27. std::cout << "{} ∩ {} ? " << check({}, {}) << "\n";
  28. std::cout << "{1, 2} ∩ {} ? " << check({1, 2}, {}) << "\n";
  29. std::cout << "{1} ∩ {1} ? " << check({1}, {1}) << "\n";
  30. return 0;
  31. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
{1, 2} ∩ {3, 4} ? false
{1, 2} ∩ {0, 2} ? true
{} ∩ {} ? false
{1, 2} ∩ {} ? false
{1} ∩ {1} ? true