fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5.  
  6. struct csv_whitespace : std::ctype<char> {
  7. static const mask* make_table() {
  8. static std::vector<mask> v(classic_table(), classic_table() + table_size);
  9. v[','] |= space;
  10. v[' '] &= ~space;
  11. return &v[0];
  12. }
  13.  
  14. csv_whitespace(std::size_t refs = 0) : std::ctype<char>{ make_table(), false, refs } { }
  15. };
  16.  
  17. int main() {
  18. std::string str("1,2,3,4,5,6,7,8,9,10");
  19. std::stringstream ss(str);
  20. auto loc = ss.getloc();
  21. ss.imbue(std::locale(loc, new csv_whitespace));
  22. std::size_t N = 3;
  23. while(N--) {
  24. std::string a;
  25. ss >> a;
  26. std::cout << a << '\n';
  27. }
  28. ss.ignore();
  29. ss.imbue(loc);
  30. std::string rest;
  31. ss >> rest;
  32. std::cout << "Rest: " << rest << "\n";
  33. }
Success #stdin #stdout 0s 4188KB
stdin
Standard input is empty
stdout
1
2
3
Rest: 4,5,6,7,8,9,10