fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. template<typename it>
  6. struct range
  7. {
  8. it i_begin;
  9. it i_end;
  10.  
  11. it begin() { return i_begin; }
  12. it end() { return i_end; }
  13.  
  14. bool empty() { return i_begin == i_end; }
  15. };
  16.  
  17. template<typename it>
  18. range<it> make_range(it i_begin, it i_end)
  19. {
  20. return {i_begin, i_end};
  21. }
  22.  
  23. template<typename t, typename range>
  24. bool contains(const t& i_find, range&& list)
  25. {
  26. return list.begin() == list.end() ? false : (*list.begin() == i_find ? true : contains(i_find, make_range(std::next(list.begin()), list.end())));
  27. }
  28.  
  29. int main() {
  30. auto list = {1,2,3,4};
  31. printf("contains 0: %i\n", contains(0, list) ? 1 : 0);
  32. printf("contains 1: %i\n", contains(1, list) ? 1 : 0);
  33. printf("contains 2: %i\n", contains(2, list) ? 1 : 0);
  34. printf("contains 3: %i\n", contains(3, list) ? 1 : 0);
  35. printf("contains 4: %i\n", contains(4, list) ? 1 : 0);
  36. printf("contains 5: %i\n", contains(5, list) ? 1 : 0);
  37. // your code goes here
  38. return 0;
  39. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
contains 0: 0
contains 1: 1
contains 2: 1
contains 3: 1
contains 4: 1
contains 5: 0