fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <locale>
  4. #include <sstream>
  5.  
  6. // This ctype facet classifies .^:;\n but not anything else as whitespace
  7. struct my_whitespace : std::ctype<char> {
  8. static const mask* make_table()
  9. {
  10. // make a copy of the "C" locale table
  11. static std::vector<mask> v(classic_table(), classic_table() + table_size);
  12. // these will be whitespace
  13. v['.'] |= space;
  14. v['^'] |= space;
  15. v[':'] |= space;
  16. v[';'] |= space;
  17. // space and tab won't be whitespace
  18. v[' '] &= ~space;
  19. v['\t'] &= ~space;
  20. return &v[0];
  21. }
  22. my_whitespace(std::size_t refs = 0) :
  23. std::ctype<char>(make_table(), false, refs) {}
  24. };
  25.  
  26. int main()
  27. {
  28. std::string in = "a:b.c;d\ne^f";
  29.  
  30. std::istringstream s(in);
  31. s.imbue(std::locale(s.getloc(), new my_whitespace()));
  32.  
  33. std::string token;
  34. while(s >> token)
  35. std::cout << " " << token<< '\n';
  36. }
Success #stdin #stdout 0.02s 2820KB
stdin
Standard input is empty
stdout
  a
  b
  c
  d
  e
  f