fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <vector>
  4. #include <string>//<string_view> doesn't work in this compiler!
  5. #include <initializer_list>
  6. #include <type_traits>
  7. #include <iterator>
  8. #include <cstdlib>
  9. #include <ctime>
  10.  
  11. template <typename Iter>
  12. void func(Iter begin, Iter end, std::random_access_iterator_tag)
  13. {
  14. size_t size = end - begin;
  15. for(int i = 0; i < 10; ++i)
  16. {
  17. std::cout << *(begin + (std::rand() % size));
  18. }
  19. }
  20.  
  21. template <typename Iter>
  22. void func(Iter begin, Iter end)
  23. {
  24. func(begin, end,
  25. typename std::iterator_traits<Iter>::iterator_category());
  26. }
  27.  
  28. template <typename Container>
  29. void func(const Container &c)
  30. {
  31. func(std::begin(c), std::end(c));
  32. }
  33.  
  34. template <typename T>
  35. void func(const std::initializer_list<T> &l)
  36. {
  37. func(l.begin(), l.end());
  38. }
  39.  
  40. int main()
  41. {
  42. std::srand(std::time(nullptr));
  43.  
  44. std::array<std::string/*_view*/, 2> a{"1","2"};
  45. func(a);
  46.  
  47. std::cout << std::endl;
  48.  
  49. std::vector<std::string/*_view*/> v{"1","2"};
  50. func(v);
  51.  
  52. std::cout << std::endl;
  53.  
  54. //using std::literals::string_view_literals;
  55. func({"1"/*sv*/, "2"/*sv*/});
  56.  
  57. std::cout << std::endl;
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 4940KB
stdin
Standard input is empty
stdout
1212112221
1111111112
2211111212