Guest User

Untitled

a guest
Jun 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. vector<string> split( const string &str, const char &delim )
  2. {
  3. typedef string::const_iterator iter;
  4.  
  5. iter beg = str.begin();
  6.  
  7. vector<string> tokens;
  8.  
  9. while(beg != str.end())
  10. {
  11. iter temp = find(beg, str.end(), delim);
  12. if(beg != str.end())
  13. tokens.push_back(string(beg, temp));
  14. beg = temp;
  15. }
  16.  
  17. return tokens;
  18. }
  19.  
  20. vector<string> x = split ("Hello there, Bob.", ' ');
  21.  
  22. #include <iostream>
  23. #include <string>
  24. #include <vector>
  25. #include <algorithm>
  26. using namespace std;
  27.  
  28. vector<string> split( const string &str, const char &delim ) {
  29. typedef string::const_iterator iter;
  30. iter beg = str.begin();
  31. vector<string> tokens;
  32.  
  33. while(beg != str.end()) {
  34. //cout << ":" << beg._Myptr << ":" << endl;
  35. iter temp = find(beg, str.end(), delim);
  36. if(beg != str.end())
  37. tokens.push_back(string(beg, temp));
  38. beg = temp;
  39. while ((beg != str.end()) && (*beg == delim))
  40. beg++;
  41. }
  42.  
  43. return tokens;
  44. }
  45.  
  46. int main () {
  47. vector<string> x = split ("Hello, my name is Bob. ", ' ');
  48. return 0;
  49. }
  50.  
  51. :Hello, my name is Bob. :
  52. : my name is Bob. :
  53. : my name is Bob. :
  54. : my name is Bob. :
  55. : my name is Bob. :
  56. : my name is Bob. :
  57. : my name is Bob. :
  58. : my name is Bob. :
  59.  
  60. :Hello, my name is Bob. :
  61. :my name is Bob. :
  62. :name is Bob. :
  63. :is Bob. :
  64. :Bob. :
  65.  
  66. std::vector<std::string> Split(const std::string &s, const std::string &d)
  67. {
  68. std::vector<std::string> v;
  69.  
  70. for (boost::split_iterator<std::string::iterator> i = boost::make_split_iterator(s, boost::first_finder(d, boost::is_iequal()));
  71. i != boost::split_iterator<std::string::iterator>();
  72. ++i) {
  73. v.push_back(boost::copy_range<std::string>(*i));
  74. }
  75.  
  76. return v;
  77. }
  78.  
  79. vector<string> split( const string &str, const char &delim )
  80. {
  81. typedef string::const_iterator iter;
  82.  
  83. iter beg = str.begin();
  84.  
  85. vector<string> tokens;
  86.  
  87. while(beg != str.end())
  88. {
  89. iter temp = find(beg, str.end(), delim);
  90. if(beg != str.end())
  91. tokens.push_back(string(beg, temp));
  92. if(temp != str.end())
  93. temp++;
  94. beg = temp;
  95. }
  96.  
  97. return tokens;
  98. }
  99.  
  100. vector<string> split( const string &str, const char &delim )
  101. {
  102. typedef string::const_iterator iter;
  103.  
  104. vector<string> tokens;
  105. iter pos = str.begin(), last = str.begin();
  106.  
  107. while(pos != str.end()) {
  108. last = pos;
  109. pos = find(pos, str.end(), delim);
  110.  
  111. if (pos != str.end()) {
  112. string token = string(last, pos);
  113. if (token.length() > 0)
  114. tokens.push_back(token);
  115.  
  116. last = ++pos;
  117. }
  118. }
  119.  
  120. string lastToken = string(last, pos);
  121. if (lastToken.length() > 0)
  122. tokens.push_back(lastToken);
  123.  
  124. return tokens;
  125. }
  126.  
  127. std::vector<std::string> &mysplit(const std::string &s, char delim, std::vector<std::string> &elems) {
  128. std::stringstream ss(s);
  129. std::string item;
  130. while(std::getline(ss, item, delim)) {
  131. elems.push_back(item);
  132. }
  133. return elems;
  134. }
  135.  
  136. string stringtobesplit = "AA/BB-CC")
  137. vector<string> tokens;
  138.  
  139. boost::split(tokens, stringtobesplit, boost::is_any_of("/-"));
  140. // tokens now holds 3 items: AA BB CC
  141.  
  142. std::vector<std::string> result;
  143. boost::iter_split(result, str, boost::first_finder(delim));
  144.  
  145. std::vector<std::string> result;
  146. boost::iter_split(result, str,
  147. boost::first_finder(delim, boost::is_iequal()));
Add Comment
Please, Sign In to add comment