Advertisement
Wolfrost

C++ String split with STL

Nov 27th, 2016
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. void split(const std::string &s, char delim, std::vector<std::string> &elems) {
  7.     std::stringstream ss;
  8.     ss.str(s);
  9.     std::string item;
  10.     while (std::getline(ss, item, delim)) {
  11.         elems.push_back(item);
  12.     }
  13. }
  14. std::vector<std::string> split(const std::string &s, char delim) {
  15.     std::vector<std::string> elems;
  16.     split(s, delim, elems);
  17.     return elems;
  18. }
  19.  
  20. int main()
  21. {
  22.     std::vector<std::string> x = split("one:two:three", ':');
  23.     for (s : x) std::cout << s << std::endl;
  24.     std::cin.get();
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement