SHARE
TWEET

Untitled

a guest May 24th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iterator>
  3. #include <vector>
  4. #include <sstream>
  5.  
  6. template<typename CharType, CharType>
  7. class DelimiterTrait : public std::basic_string<CharType>
  8. {
  9. };
  10.  
  11. template<typename CharType, CharType delimiter>
  12. std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& is, DelimiterTrait<CharType, delimiter>& output)
  13. {
  14.     std::getline(is, output, delimiter);
  15.     return is;
  16. }
  17.  
  18. template<typename CharType, CharType delimiter>
  19. void Split(std::basic_string<CharType> line, std::vector<std::basic_string<CharType>>& dst)
  20. {
  21.     std::basic_istringstream<CharType> iss(std::move(line));
  22.     std::vector<std::basic_string<CharType>> result((std::istream_iterator<DelimiterTrait<CharType, delimiter>, CharType>(iss)),
  23.         std::istream_iterator<DelimiterTrait<CharType, delimiter>, CharType>());
  24.     std::swap(result, dst);
  25. }
  26.  
  27. template<char delimiter>
  28. void Split(std::string line, std::vector<std::string>& dst)
  29. {
  30.     Split<char,delimiter>(std::move(line), dst);
  31. }
  32.  
  33. template<wchar_t delimiter>
  34. void Split(std::wstring line, std::vector<std::wstring>& dst)
  35. {
  36.     Split<wchar_t,delimiter>(std::move(line), dst);
  37. }
  38.  
  39. int main()
  40. {
  41.     {
  42.         std::wstring text = L"L;Str;Foo;bar;tar;space; ;words";
  43.         std::vector<std::wstring> results;
  44.         Split<L';'>(text, results);
  45.    
  46.         for(auto&& v: results)
  47.             std::wcout << v << std::endl;
  48.     }
  49.    
  50.     {
  51.         std::string text = "Foo;bar;tar;space; ;words";
  52.         std::vector<std::string> results;
  53.         Split<';'>(text, results);
  54.    
  55.         for(auto&& v: results)
  56.             std::cout << v << std::endl;
  57.     }
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top