fork(4) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <regex>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. std::regex operator ""_re (char const * const str, std::size_t)
  9. {
  10. return std::regex { str };
  11. }
  12.  
  13. class split
  14. {
  15. public:
  16. split(std::regex splitter, std::string original)
  17. : splitter_ { std::move(splitter) }
  18. , original_ { std::move(original) }
  19. {
  20. }
  21.  
  22. auto begin() const
  23. {
  24. return std::sregex_token_iterator { original_.begin(), original_.end(), splitter_, -1 };
  25. }
  26.  
  27. auto end() const
  28. {
  29. return std::sregex_token_iterator {};
  30. }
  31.  
  32. template <typename Container>
  33. operator Container () const
  34. {
  35. return { begin(), end() };
  36. }
  37.  
  38. private:
  39. std::regex splitter_;
  40. std::string original_;
  41. };
  42.  
  43. int main()
  44. {
  45. using namespace std::literals::string_literals;
  46.  
  47. std::vector<std::string> const words = split {
  48. R"(\s+)"_re,
  49. "hello\tdarkness my\nold friend"s
  50. };
  51.  
  52. for (auto const & word : words)
  53. std::cout << word << "\n";
  54.  
  55. for (auto const & number : split { ","_re, "23,42,1337" })
  56. std::cout << number << "\n";
  57.  
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 3560KB
stdin
Standard input is empty
stdout
hello
darkness
my
old
friend
23
42
1337