#include <iostream>
#include <vector>
void dump(std::ostream &out, const std::vector<std::string> &v)
{
for(size_t i = 0; i < v.size(); ++i) {
out << '\'' << v[ i ] << '\'' << ' ';
}
out << std::endl;
}
size_t split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
size_t pos = txt.find( ch );
size_t initialPos = 0;
strs.clear();
// Decompose statement
while( pos != std::string::npos ) {
strs.push_back( txt.substr( initialPos, pos - initialPos ) );
initialPos = pos + 1;
pos = txt.find( ch, initialPos );
}
// Add the last one
strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
return strs.size();
}
int main()
{
std::vector<std::string> v;
split( "This is a test", v, ' ' );
dump( std::cout, v );
split( "1,2,3, 4, 5, 6, 7,8,9", v, ',' );
dump( std::cout, v );
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKCnZvaWQgZHVtcChzdGQ6Om9zdHJlYW0gJm91dCwgY29uc3Qgc3RkOjp2ZWN0b3I8c3RkOjpzdHJpbmc+ICZ2KQp7CiAgICBmb3Ioc2l6ZV90IGkgPSAwOyBpIDwgdi5zaXplKCk7ICsraSkgewogICAgICAgIG91dCA8PCAnXCcnIDw8IHZbIGkgXSA8PCAnXCcnIDw8ICcgJzsKICAgIH0KICAgIAogICAgb3V0IDw8IHN0ZDo6ZW5kbDsKfQoKc2l6ZV90IHNwbGl0KGNvbnN0IHN0ZDo6c3RyaW5nICZ0eHQsIHN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiAmc3RycywgY2hhciBjaCkKewogICAgc2l6ZV90IHBvcyA9IHR4dC5maW5kKCBjaCApOwogICAgc2l6ZV90IGluaXRpYWxQb3MgPSAwOwogICAgc3Rycy5jbGVhcigpOwoKICAgIC8vIERlY29tcG9zZSBzdGF0ZW1lbnQKICAgIHdoaWxlKCBwb3MgIT0gc3RkOjpzdHJpbmc6Om5wb3MgKSB7CiAgICAgICAgc3Rycy5wdXNoX2JhY2soIH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