Check this out :: C ++ Cookbook Line Split (Recipe 4.6)
Example 4-10 Split a line with delimiters
#include <string> #include <vector> #include <functional> #include <iostream> using namespace std; void split(const string& s, char c, vector<string>& v) { string::size_type i = 0; string::size_type j = s.find(c); while (j != string::npos) { v.push_back(s.substr(i, ji)); i = ++j; j = s.find(c, j); if (j == string::npos) v.push_back(s.substr(i, s.length( ))); } } int main( ) { vector<string> v; string s = "Account Name|Address 1|Address 2|City"; split(s, '|', v); for (int i = 0; i < v.size( ); ++i) { cout << v[i] << '\n'; } }
-
template<typename T> void split(const basic_string<T>& s, T c, vector<basic_string<T> >& v) { basic_string<T>::size_type i = 0; basic_string<T>::size_type j = s.find(c); while (j != basic_string<T>::npos) { v.push_back(s.substr(i, ji)); i = ++j; j = s.find(c, j); if (j == basic_string<T>::npos) v.push_back(s.substr(i, s.length( ))); } }
Example 4-11 Splitting a string using Boost
#include <iostream> #include <string> #include <list> #include <boost/algorithm/string.hpp> using namespace std; using namespace boost; int main( ) { string s = "one,two,three,four"; list<string> results; split(results, s, is_any_of(",")); // Note this is boost::split for (list<string>::const_iterator p = results.begin( ); p != results.end( ); ++p) { cout << *p << endl; } }
-
template<typename Seq, typename Coll, typename Pred> Seq& split(Seq& s, Coll& c, Pred p, token_compress_mode_type e = token_compress_off);
I can’t share the text (it’s illegal to copy / paste from a book), but these examples are quite explanatory. If you want to see the text, you will need to specify the book.
These two examples were taken from recipe 4.6.
C ++ cookbook
Jeff Cogswell, Christopher Diggins, Ryan Stevens, Jonathan Turkanis
Publisher: O'Reilly
Publication Date: November 2005
ISBN: 0-596-00761-2