Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string s("Somewhere down the road");
  10. istringstream iss(s);
  11.  
  12. do
  13. {
  14. string sub;
  15. iss >> sub;
  16. cout << "Substring: " << sub << endl;
  17.  
  18. } while (iss);
  19.  
  20. }
  21.  
  22. #include <iostream>
  23. #include <string>
  24. #include <sstream>
  25. #include <algorithm>
  26. #include <iterator>
  27.  
  28. int main() {
  29. using namespace std;
  30. string sentence = "And I feel fine...";
  31. istringstream iss(sentence);
  32. copy(istream_iterator<string>(iss),
  33. istream_iterator<string>(),
  34. ostream_iterator<string>(cout, "n"));
  35. }
  36.  
  37. vector<string> tokens;
  38. copy(istream_iterator<string>(iss),
  39. istream_iterator<string>(),
  40. back_inserter(tokens));
  41.  
  42. vector<string> tokens{istream_iterator<string>{iss},
  43. istream_iterator<string>{}};
  44.  
  45. #include <boost/algorithm/string.hpp>
  46. std::vector<std::string> strs;
  47. boost::split(strs, "string to split", boost::is_any_of("t "));
  48.  
  49. #include <vector>
  50. #include <string>
  51. #include <sstream>
  52.  
  53. using namespace std;
  54.  
  55. int main()
  56. {
  57. string str("Split me by whitespaces");
  58. string buf; // Have a buffer string
  59. stringstream ss(str); // Insert the string into a stream
  60.  
  61. vector<string> tokens; // Create vector to hold our words
  62.  
  63. while (ss >> buf)
  64. tokens.push_back(buf);
  65. }
  66.  
  67. template < class ContainerT >
  68. void tokenize(const std::string& str, ContainerT& tokens,
  69. const std::string& delimiters = " ", bool trimEmpty = false)
  70. {
  71. std::string::size_type pos, lastPos = 0;
  72.  
  73. using value_type = typename ContainerT::value_type;
  74. using size_type = typename ContainerT::size_type;
  75.  
  76. while(true)
  77. {
  78. pos = str.find_first_of(delimiters, lastPos);
  79. if(pos == std::string::npos)
  80. {
  81. pos = str.length();
  82.  
  83. if(pos != lastPos || !trimEmpty)
  84. tokens.push_back(value_type(str.data()+lastPos,
  85. (size_type)pos-lastPos ));
  86.  
  87. break;
  88. }
  89. else
  90. {
  91. if(pos != lastPos || !trimEmpty)
  92. tokens.push_back(value_type(str.data()+lastPos,
  93. (size_type)pos-lastPos ));
  94. }
  95.  
  96. lastPos = pos + 1;
  97. }
  98. }
  99.  
  100. string line = "a line of text to iterate through";
  101. string word;
  102.  
  103. istringstream iss(line, istringstream::in);
  104.  
  105. while( iss >> word )
  106. {
  107.  
  108. ...
  109.  
  110. }
  111.  
  112. #include <iostream>
  113. #include <string>
  114. #include <boost/foreach.hpp>
  115. #include <boost/tokenizer.hpp>
  116.  
  117. using namespace std;
  118. using namespace boost;
  119.  
  120. int main(int argc, char** argv)
  121. {
  122. string text = "token testtstring";
  123.  
  124. char_separator<char> sep(" t");
  125. tokenizer<char_separator<char> > tokens(text, sep);
  126. BOOST_FOREACH(string t, tokens)
  127. {
  128. cout << t << "." << endl;
  129. }
  130. }
  131.  
  132. void split(vector<string> &tokens, const string &text, char sep) {
  133. int start = 0, end = 0;
  134. while ((end = text.find(sep, start)) != string::npos) {
  135. tokens.push_back(text.substr(start, end - start));
  136. start = end + 1;
  137. }
  138. tokens.push_back(text.substr(start));
  139. }
  140.  
  141. #include <iostream>
  142. #include <string>
  143. #include <vector>
  144. #include <algorithm>
  145. #include <iterator>
  146. using namespace std;
  147.  
  148. vector<string> split(const string& s, const string& delim, const bool keep_empty = true) {
  149. vector<string> result;
  150. if (delim.empty()) {
  151. result.push_back(s);
  152. return result;
  153. }
  154. string::const_iterator substart = s.begin(), subend;
  155. while (true) {
  156. subend = search(substart, s.end(), delim.begin(), delim.end());
  157. string temp(substart, subend);
  158. if (keep_empty || !temp.empty()) {
  159. result.push_back(temp);
  160. }
  161. if (subend == s.end()) {
  162. break;
  163. }
  164. substart = subend + delim.size();
  165. }
  166. return result;
  167. }
  168.  
  169. int main() {
  170. const vector<string> words = split("So close no matter how far", " ");
  171. copy(words.begin(), words.end(), ostream_iterator<string>(cout, "n"));
  172. }
  173.  
  174. template<typename Operator>
  175. void tokenize(Operator& op, const char* input, const char* delimiters) {
  176. const char* s = input;
  177. const char* e = s;
  178. while (*e != 0) {
  179. e = s;
  180. while (*e != 0 && strchr(delimiters, *e) == 0) ++e;
  181. if (e - s > 0) {
  182. op(s, e - s);
  183. }
  184. s = e + 1;
  185. }
  186. }
  187.  
  188. template<class ContainerType>
  189. class Appender {
  190. public:
  191. Appender(ContainerType& container) : container_(container) {;}
  192. void operator() (const char* s, unsigned length) {
  193. container_.push_back(std::string(s,length));
  194. }
  195. private:
  196. ContainerType& container_;
  197. };
  198.  
  199. std::vector<std::string> strVector;
  200. Appender v(strVector);
  201. tokenize(v, "A number of words to be tokenized", " t");
  202.  
  203. class WordCounter {
  204. public:
  205. WordCounter() : noOfWords(0) {}
  206. void operator() (const char*, unsigned) {
  207. ++noOfWords;
  208. }
  209. unsigned noOfWords;
  210. };
  211.  
  212. WordCounter wc;
  213. tokenize(wc, "A number of words to be counted", " t");
  214. ASSERT( wc.noOfWords == 7 );
  215.  
  216. void Tokenize(const string& str,
  217. vector<string>& tokens,
  218. const string& delimiters = " ")
  219. {
  220. // Skip delimiters at beginning.
  221. string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  222. // Find first "non-delimiter".
  223. string::size_type pos = str.find_first_of(delimiters, lastPos);
  224.  
  225. while (string::npos != pos || string::npos != lastPos)
  226. {
  227. // Found a token, add it to the vector.
  228. tokens.push_back(str.substr(lastPos, pos - lastPos));
  229. // Skip delimiters. Note the "not_of"
  230. lastPos = str.find_first_not_of(delimiters, pos);
  231. // Find next "non-delimiter"
  232. pos = str.find_first_of(delimiters, lastPos);
  233. }
  234. }
  235.  
  236. template<typename T>
  237. vector<T>
  238. split(const T & str, const T & delimiters) {
  239. vector<T> v;
  240. T::size_type start = 0;
  241. auto pos = str.find_first_of(delimiters, start);
  242. while(pos != T::npos) {
  243. if(pos != start) // ignore empty tokens
  244. v.emplace_back(str, start, pos - start);
  245. start = pos + 1;
  246. pos = str.find_first_of(delimiters, start);
  247. }
  248. if(start < str.length()) // ignore trailing delimiter
  249. v.emplace_back(str, start, str.length() - start); // add what's left of the string
  250. return v;
  251. }
  252.  
  253. vector<string> v = split<string>("Hello, there; World", ";,");
  254. vector<wstring> v = split<wstring>(L"Hello, there; World", L";,");
  255.  
  256. #include <iostream>
  257. #include <vector>
  258. #include <boost/algorithm/string.hpp>
  259.  
  260. template<typename _OutputIterator>
  261. inline void split(
  262. const std::string& str,
  263. const std::string& delim,
  264. _OutputIterator result)
  265. {
  266. using namespace boost::algorithm;
  267. typedef split_iterator<std::string::const_iterator> It;
  268.  
  269. for(It iter=make_split_iterator(str, first_finder(delim, is_equal()));
  270. iter!=It();
  271. ++iter)
  272. {
  273. *(result++) = boost::copy_range<std::string>(*iter);
  274. }
  275. }
  276.  
  277. int main(int argc, char* argv[])
  278. {
  279. using namespace std;
  280.  
  281. vector<string> splitted;
  282. split("HelloFOOworldFOO!", "FOO", back_inserter(splitted));
  283.  
  284. // or directly to console, for example
  285. split("HelloFOOworldFOO!", "FOO", ostream_iterator<string>(cout, "n"));
  286. return 0;
  287. }
  288.  
  289. char sep = ' ';
  290. std::string s="1 This is an example";
  291.  
  292. for(size_t p=0, q=0; p!=s.npos; p=q)
  293. std::cout << s.substr(p+(p!=0), (q=s.find(sep, p+1))-p-(p!=0)) << std::endl;
  294.  
  295. #include<string>
  296. using namespace std;
  297.  
  298. vector<string> split(char* str,const char* delim)
  299. {
  300. char* saveptr;
  301. char* token = strtok_r(str,delim,&saveptr);
  302.  
  303. vector<string> result;
  304.  
  305. while(token != NULL)
  306. {
  307. result.push_back(token);
  308. token = strtok_r(NULL,delim,&saveptr);
  309. }
  310. return result;
  311. }
  312.  
  313. #include <iostream>
  314. #include <string>
  315.  
  316. int main()
  317. {
  318. std::string s("Somewhere down the road");
  319.  
  320. std::string::size_type prev_pos = 0, pos = 0;
  321. while( (pos = s.find(' ', pos)) != std::string::npos )
  322. {
  323. std::string substring( s.substr(prev_pos, pos-prev_pos) );
  324.  
  325. std::cout << substring << 'n';
  326.  
  327. prev_pos = ++pos;
  328. }
  329. std::string substring( s.substr(prev_pos, pos-prev_pos) ); // Last word
  330. std::cout << substring << 'n';
  331. }
  332.  
  333. static void Split(std::vector<std::string>& lst, const std::string& input, const std::string& separators, bool remove_empty = true)
  334. {
  335. std::ostringstream word;
  336. for (size_t n = 0; n < input.size(); ++n)
  337. {
  338. if (std::string::npos == separators.find(input[n]))
  339. word << input[n];
  340. else
  341. {
  342. if (!word.str().empty() || !remove_empty)
  343. lst.push_back(word.str());
  344. word.str("");
  345. }
  346. }
  347. if (!word.str().empty() || !remove_empty)
  348. lst.push_back(word.str());
  349. }
  350.  
  351. #include <vector>
  352. #include <iostream>
  353. #include <string.h>
  354. using namespace std;
  355. class StringSplit
  356. {
  357. private:
  358. void copy_fragment(char*,char*,char*);
  359. void copy_fragment(char*,char*,char);
  360. bool match_fragment(char*,char*,int);
  361. int untilnextdelim(char*,char);
  362. int untilnextdelim(char*,char*);
  363. void assimilate(char*,char);
  364. void assimilate(char*,char*);
  365. bool string_contains(char*,char*);
  366. long calc_string_size(char*);
  367. void copy_string(char*,char*);
  368.  
  369. public:
  370. vector<char*> split_cstr(char);
  371. vector<char*> split_cstr(char*);
  372. vector<string> split_string(char);
  373. vector<string> split_string(char*);
  374. char* String;
  375. bool do_string;
  376. bool keep_empty;
  377. vector<char*> Container;
  378. vector<string> ContainerS;
  379. StringSplit(char * in)
  380. {
  381. String = in;
  382. }
  383. StringSplit(string in)
  384. {
  385. size_t len = calc_string_size((char*)in.c_str());
  386. String = new char[len+1];
  387. memset(String,0,len+1);
  388. copy_string(String,(char*)in.c_str());
  389. do_string = true;
  390. }
  391. ~StringSplit()
  392. {
  393. for(int i = 0;i<Container.size();i++)
  394. if(Container[i]!=NULL)
  395. delete[] Container[i];
  396. if(do_string)
  397. delete[] String;
  398. }
  399.  
  400. #include <string.h>
  401. #include <iostream>
  402. #include <vector>
  403. #include "StringSplit.hpp"
  404. using namespace std;
  405.  
  406. void StringSplit::assimilate(char*src,char delim)
  407. {
  408. int until = untilnextdelim(src,delim);
  409. if(until>0)
  410. {
  411. char * temp = new char[until+1];
  412. memset(temp,0,until+1);
  413. copy_fragment(temp,src,delim);
  414. if(keep_empty||*temp!=0)
  415. {
  416. if(!do_string)
  417. {
  418. Container.push_back(temp);
  419. }
  420. else
  421. {
  422. string x = temp;
  423. ContainerS.push_back(x);
  424. }
  425.  
  426. }
  427. else
  428. {
  429. delete[] temp;
  430. }
  431.  
  432. }
  433.  
  434. }
  435. void StringSplit::assimilate(char*src,char* delim)
  436. {
  437. int until = untilnextdelim(src,delim);
  438. if(until>0)
  439. {
  440. char * temp = new char[until+1];
  441. memset(temp,0,until+1);
  442. copy_fragment(temp,src,delim);
  443. if(keep_empty||*temp!=0)
  444. {
  445. if(!do_string)
  446. {
  447. Container.push_back(temp);
  448. }
  449. else
  450. {
  451. string x = temp;
  452. ContainerS.push_back(x);
  453. }
  454.  
  455. }
  456. else
  457. {
  458. delete[] temp;
  459. }
  460.  
  461. }
  462.  
  463. }
  464. long StringSplit::calc_string_size(char* _in)
  465. {
  466. long i = 0;
  467. while(*_in++)
  468. i++;
  469. return i;
  470. }
  471. bool StringSplit::string_contains(char*haystack,char*needle)
  472. {
  473. size_t len = calc_string_size(needle);
  474. size_t lenh = calc_string_size(haystack);
  475. while(lenh--)
  476. if(match_fragment(haystack+lenh,needle,len))
  477. return true;
  478. return false;
  479. }
  480. bool StringSplit::match_fragment(char* _src, char*cmp,int len)
  481. {
  482. while(len--)
  483. if(*(_src+len)!=*(cmp+len))
  484. return false;
  485. return true;
  486. }
  487. int StringSplit::untilnextdelim(char* _in, char delim)
  488. {
  489. size_t len = calc_string_size(_in);
  490. if(*_in==delim)
  491. {
  492. _in += 1;
  493. return len-1;
  494. }
  495. int c = 0;
  496. while(*(_in+c)!=delim&&c<len)
  497. c++;
  498. return c;
  499. }
  500. int StringSplit::untilnextdelim(char* _in, char* delim)
  501. {
  502. int s = calc_string_size(delim);
  503. int c = 1 + s;
  504. if(!string_contains(_in,delim))
  505. {
  506. return calc_string_size(_in);
  507. }
  508. else if(match_fragment(_in,delim,s))
  509. {
  510. _in+=s;
  511. return calc_string_size(_in);
  512. }
  513. while(!match_fragment(_in+c,delim,s))
  514. c++;
  515. return c;
  516. }
  517. void StringSplit::copy_fragment(char*dest,char*src,char delim)
  518. {
  519. if(*src==delim)
  520. src++;
  521. int c = 0;
  522. while(*(src+c) != delim && *(src+c))
  523. {
  524. *(dest+c) = *(src+c);
  525. c++;
  526. }
  527. *(dest+c)=0;
  528. }
  529. void StringSplit::copy_string(char*dest,char*src)
  530. {
  531. int i = 0;
  532. while(*(src+i))
  533. {
  534. *(dest+i) = *(src+i);
  535. i++;
  536. }
  537. }
  538. void StringSplit::copy_fragment(char*dest,char*src,char*delim)
  539. {
  540. size_t len = calc_string_size(delim);
  541. size_t lens = calc_string_size(src);
  542. if(match_fragment(src,delim,len))
  543. {
  544. src += len;
  545. lens -= len;
  546. }
  547. int c = 0;
  548. while(!match_fragment(src+c,delim,len) && c<lens)
  549. {
  550. *(dest+c) = *(src+c);
  551. c++;
  552. }
  553. *(dest+c)=0;
  554. }
  555.  
  556. vector<char*> StringSplit::split_cstr(char Delimiter)
  557. {
  558. int i = 0;
  559. while(*String)
  560. {
  561. if(*String!=Delimiter && i == 0)
  562. assimilate(String,Delimiter);
  563. if(*String==Delimiter)
  564. assimilate(String,Delimiter);
  565. i++;
  566. String++;
  567. }
  568. String -= i;
  569. delete[] String;
  570. return Container;
  571. }
  572. vector<string> StringSplit::split_string(char Delimiter)
  573. {
  574. do_string = true;
  575. int i = 0;
  576. while(*String)
  577. {
  578. if(*String!=Delimiter && i == 0)
  579. assimilate(String,Delimiter);
  580. if(*String==Delimiter)
  581. assimilate(String,Delimiter);
  582. i++;
  583. String++;
  584. }
  585. String -= i;
  586. delete[] String;
  587. return ContainerS;
  588. }
  589. vector<char*> StringSplit::split_cstr(char* Delimiter)
  590. {
  591. int i = 0;
  592. size_t LenDelim = calc_string_size(Delimiter);
  593. while(*String)
  594. {
  595. if(!match_fragment(String,Delimiter,LenDelim) && i == 0)
  596. assimilate(String,Delimiter);
  597. if(match_fragment(String,Delimiter,LenDelim))
  598. assimilate(String,Delimiter);
  599. i++;
  600. String++;
  601.  
  602. }
  603. String -= i;
  604. delete[] String;
  605. return Container;
  606. }
  607. vector<string> StringSplit::split_string(char* Delimiter)
  608. {
  609. do_string = true;
  610. int i = 0;
  611. size_t LenDelim = calc_string_size(Delimiter);
  612. while(*String)
  613. {
  614. if(!match_fragment(String,Delimiter,LenDelim) && i == 0)
  615. assimilate(String,Delimiter);
  616. if(match_fragment(String,Delimiter,LenDelim))
  617. assimilate(String,Delimiter);
  618. i++;
  619. String++;
  620.  
  621. }
  622. String -= i;
  623. delete[] String;
  624. return ContainerS;
  625. }
  626.  
  627. int main(int argc, char*argv[])
  628. {
  629. StringSplit ss = "This:CUT:is:CUT:an:CUT:example:CUT:cstring";
  630. vector<char*> Split = ss.split_cstr(":CUT:");
  631. for(int i = 0;i<Split.size();i++)
  632. cout << Split[i] << endl;
  633. return 0;
  634. }
  635.  
  636. int main(int argc, char*argv[])
  637. {
  638. StringSplit ss = "This:is:an:example:cstring";
  639. vector<char*> Split = ss.split_cstr(':');
  640. for(int i = 0;i<Split.size();i++)
  641. cout << Split[i] << endl;
  642. return 0;
  643. }
  644.  
  645. int main(int argc, char*argv[])
  646. {
  647. string mystring = "This[SPLIT]is[SPLIT]an[SPLIT]example[SPLIT]string"
  648. StringSplit ss = mystring;
  649. vector<string> Split = ss.split_string("[SPLIT]");
  650. for(int i = 0;i<Split.size();i++)
  651. cout << Split[i] << endl;
  652. return 0;
  653. }
  654.  
  655. int main(int argc, char*argv[])
  656. {
  657. string mystring = "This|is|an|example|string"
  658. StringSplit ss = mystring;
  659. vector<string> Split = ss.split_string('|');
  660. for(int i = 0;i<Split.size();i++)
  661. cout << Split[i] << endl;
  662. return 0;
  663. }
  664.  
  665. StringSplit ss = mystring;
  666. ss.keep_empty = true;
  667. vector<string> Split = ss.split_string(":DELIM:");
  668.  
  669. String[] Split = "Hey:cut:what's:cut:your:cut:name?".Split(new[]{":cut:"},StringSplitOptions.None);
  670. foreach(String X in Split)
  671. Write(X);
  672.  
  673. #include <regex.h>
  674. #include <string.h>
  675. #include <vector.h>
  676.  
  677. using namespace std;
  678.  
  679. vector<string> split(string s){
  680. regex r ("\w+"); //regex matches whole words, (greedy, so no fragment words)
  681. regex_iterator<string::iterator> rit ( s.begin(), s.end(), r );
  682. regex_iterator<string::iterator> rend; //iterators to iterate thru words
  683. vector<string> result<regex_iterator>(rit, rend);
  684. return result; //iterates through the matches to fill the vector
  685. }
  686.  
  687. #include <iostream>
  688. #include <vector>
  689. #include <string>
  690. #include <strtk.hpp>
  691.  
  692. const char *whitespace = " trnf";
  693. const char *whitespace_and_punctuation = " trnf;,=";
  694.  
  695. int main()
  696. {
  697. { // normal parsing of a string into a vector of strings
  698. std::string s("Somewhere down the road");
  699. std::vector<std::string> result;
  700. if( strtk::parse( s, whitespace, result ) )
  701. {
  702. for(size_t i = 0; i < result.size(); ++i )
  703. std::cout << result[i] << std::endl;
  704. }
  705. }
  706.  
  707. { // parsing a string into a vector of floats with other separators
  708. // besides spaces
  709.  
  710. std::string s("3.0, 3.14; 4.0");
  711. std::vector<float> values;
  712. if( strtk::parse( s, whitespace_and_punctuation, values ) )
  713. {
  714. for(size_t i = 0; i < values.size(); ++i )
  715. std::cout << values[i] << std::endl;
  716. }
  717. }
  718.  
  719. { // parsing a string into specific variables
  720.  
  721. std::string s("angle = 45; radius = 9.9");
  722. std::string w1, w2;
  723. float v1, v2;
  724. if( strtk::parse( s, whitespace_and_punctuation, w1, v1, w2, v2) )
  725. {
  726. std::cout << "word " << w1 << ", value " << v1 << std::endl;
  727. std::cout << "word " << w2 << ", value " << v2 << std::endl;
  728. }
  729. }
  730.  
  731. return 0;
  732. }
  733.  
  734. void split_string(string text,vector<string>& words)
  735. {
  736. int i=0;
  737. char ch;
  738. string word;
  739.  
  740. while(ch=text[i++])
  741. {
  742. if (isspace(ch))
  743. {
  744. if (!word.empty())
  745. {
  746. words.push_back(word);
  747. }
  748. word = "";
  749. }
  750. else
  751. {
  752. word += ch;
  753. }
  754. }
  755. if (!word.empty())
  756. {
  757. words.push_back(word);
  758. }
  759. }
  760.  
  761. string s = "Name:JAck; Spouse:Susan; ...";
  762. string dummy, name, spouse;
  763.  
  764. istringstream iss(s);
  765. getline(iss, dummy, ':');
  766. getline(iss, name, ';');
  767. getline(iss, dummy, ':');
  768. getline(iss, spouse, ';')
  769.  
  770. #include <iostream>
  771. #include <string>
  772. #include <boost/regex.hpp>
  773.  
  774. int main() {
  775. std::string line("A:::line::to:split");
  776. const boost::regex re(":+"); // one or more colons
  777.  
  778. // -1 means find inverse matches aka split
  779. boost::sregex_token_iterator tokens(line.begin(),line.end(),re,-1);
  780. boost::sregex_token_iterator end;
  781.  
  782. for (; tokens != end; ++tokens)
  783. std::cout << *tokens << std::endl;
  784. }
  785.  
  786. #include <string>
  787. #include <vector>
  788.  
  789. using namespace std;
  790.  
  791. vector<string> split(string str, const char delim) {
  792. vector<string> v;
  793. string tmp;
  794.  
  795. for(string::const_iterator i; i = str.begin(); i <= str.end(); ++i) {
  796. if(*i != delim && i != str.end()) {
  797. tmp += *i;
  798. } else {
  799. v.push_back(tmp);
  800. tmp = "";
  801. }
  802. }
  803.  
  804. return v;
  805. }
  806.  
  807. void splitString(const String &s, const String &delim, std::vector<String> &result) {
  808. const int l = delim.length();
  809. int f = 0;
  810. int i = s.indexOf(delim,f);
  811. while (i>=0) {
  812. String token( i-f > 0 ? s.substring(f,i-f) : "");
  813. result.push_back(token);
  814. f=i+l;
  815. i = s.indexOf(delim,f);
  816. }
  817. String token = s.substring(f);
  818. result.push_back(token);
  819. }
  820.  
  821. #include <string>
  822. #include <list>
  823. #include <locale> // std::isupper
  824.  
  825. template<class String>
  826. const std::list<String> split_camel_case_string(const String &s)
  827. {
  828. std::list<String> R;
  829. String w;
  830.  
  831. for (String::const_iterator i = s.begin(); i < s.end(); ++i) { {
  832. if (std::isupper(*i)) {
  833. if (w.length()) {
  834. R.push_back(w);
  835. w.clear();
  836. }
  837. }
  838. w += *i;
  839. }
  840.  
  841. if (w.length())
  842. R.push_back(w);
  843. return R;
  844. }
  845.  
  846. #include <regex>
  847. #include <string>
  848. #include <vector>
  849.  
  850. std::vector<string> Tokenize( const string str, const std::regex regex )
  851. {
  852. using namespace std;
  853.  
  854. std::vector<string> result;
  855.  
  856. sregex_token_iterator it( str.begin(), str.end(), regex, -1 );
  857. sregex_token_iterator reg_end;
  858.  
  859. for ( ; it != reg_end; ++it ) {
  860. if ( !it->str().empty() ) //token could be empty:check
  861. result.emplace_back( it->str() );
  862. }
  863.  
  864. return result;
  865. }
  866.  
  867. std::vector<string> TokenizeDefault( const string str )
  868. {
  869. using namespace std;
  870.  
  871. regex re( "[\s,]+" );
  872.  
  873. return Tokenize( str, re );
  874. }
  875.  
  876. #pragma once
  877. #include <vector>
  878. #include <sstream>
  879. using namespace std;
  880. class Helpers
  881. {
  882. public:
  883. static vector<string> split(string s, char delim)
  884. {
  885. stringstream temp (stringstream::in | stringstream::out);
  886. vector<string> elems(0);
  887. if (s.size() == 0 || delim == 0)
  888. return elems;
  889. for each(char c in s)
  890. {
  891. if(c == delim)
  892. {
  893. elems.push_back(temp.str());
  894. temp = stringstream(stringstream::in | stringstream::out);
  895. }
  896. else
  897. temp << c;
  898. }
  899. if (temp.str().size() > 0)
  900. elems.push_back(temp.str());
  901. return elems;
  902. }
  903.  
  904. //Splits string s with a list of delimiters in delims (it's just a list, like if we wanted to
  905. //split at the following letters, a, b, c we would make delims="abc".
  906. static vector<string> split(string s, string delims)
  907. {
  908. stringstream temp (stringstream::in | stringstream::out);
  909. vector<string> elems(0);
  910. bool found;
  911. if(s.size() == 0 || delims.size() == 0)
  912. return elems;
  913. for each(char c in s)
  914. {
  915. found = false;
  916. for each(char d in delims)
  917. {
  918. if (c == d)
  919. {
  920. elems.push_back(temp.str());
  921. temp = stringstream(stringstream::in | stringstream::out);
  922. found = true;
  923. break;
  924. }
  925. }
  926. if(!found)
  927. temp << c;
  928. }
  929. if(temp.str().size() > 0)
  930. elems.push_back(temp.str());
  931. return elems;
  932. }
  933. };
  934.  
  935. 0 <len:0>
  936. 1 PICK <len:4>
  937. 2 ANY <len:3>
  938. 3 TWO: <len:4>
  939. 4 <len:0>
  940.  
  941. vector <string> split(const string& str, const string& delimiter = " ") {
  942. vector <string> tokens;
  943.  
  944. string::size_type lastPos = 0;
  945. string::size_type pos = str.find(delimiter, lastPos);
  946.  
  947. while (string::npos != pos) {
  948. // Found a token, add it to the vector.
  949. cout << str.substr(lastPos, pos - lastPos) << endl;
  950. tokens.push_back(str.substr(lastPos, pos - lastPos));
  951. lastPos = pos + delimiter.size();
  952. pos = str.find(delimiter, lastPos);
  953. }
  954.  
  955. tokens.push_back(str.substr(lastPos, str.size() - lastPos));
  956. return tokens;
  957. }
  958.  
  959. #include <boost/algorithm/string/split.hpp>
  960. #include <boost/algorithm/string.hpp>
  961. #include <iostream>
  962. #include <vector>
  963.  
  964. using namespace std;
  965. using namespace boost;
  966.  
  967. int main(int argc, char**argv) {
  968. typedef vector < string > list_type;
  969.  
  970. list_type list;
  971. string line;
  972.  
  973. line = "Somewhere down the road";
  974. split(list, line, is_any_of(" "));
  975.  
  976. for(int i = 0; i < list.size(); i++)
  977. {
  978. cout << list[i] << endl;
  979. }
  980.  
  981. return 0;
  982. }
  983.  
  984. Somewhere
  985. down
  986. the
  987. road
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement