fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. typedef wchar_t CharType;
  8. typedef std::basic_string<CharType> StringType;
  9. typedef std::vector<StringType> ContainerType;
  10. typedef std::basic_istringstream<CharType> IStringStreamType;
  11.  
  12. void SplitString(StringType const &str, CharType delim, ContainerType &collection)
  13. {
  14. IStringStreamType ss(str);
  15. while (ss.good())
  16. {
  17. StringType token;
  18. std::getline(ss, token, delim);
  19. collection.push_back(token);
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. const CharType * const theString = L"select * from table1; select * from table2;";
  26. ContainerType col;
  27. SplitString(theString, L';', col);
  28. std::for_each(
  29. col.begin()
  30. , col.end()
  31. , [] (StringType const &s) { std::wcout << s << std::endl; }
  32. );
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 3080KB
stdin
Standard input is empty
stdout
select * from table1
 select * from table2