I need a cpp function (without using boost, obv) that split a string in parts using an other string as token.
Example input:
[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]Center: (8.37, 9.86); Radius: 18.13; Point: (17.83, 6.51)[/code]
Desidered output:
[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]cks[0] = “8.37”;
cks[1] = “9.86”;
cks[2] = “18.13”;
cks[3] = “17.83”;
cks[4] = “6.51”;[/code]
How to split?
[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]
vector< string > cks = chunkize ( line, “Center: (, ); Radius: (, ); Poi: (, );” );
[/code]
Certainly not the best method, but works as I want.
[code lang=”cpp” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]
vector< string > chunkize ( const string& str, const string& tokens )
{
vector< string > cks;size_t prev = 0, pos;
while ((pos = str.find_first_of(tokens, prev)) != string::npos)
{
if ( pos > prev )
cks.push_back( str.substr(prev, pos–prev) );
prev = pos + 1;
}
if ( prev < str.length() )
cks.push_back( str.substr(prev, string::npos) );
return cks;
}
[/code]
As usual, I put here because… I have poor memory!
ref : albertopasca.it