What is the most efficient C++ method to split a string based on a particular delimiter similar to split method in python? [closed]

First, don’t use strtok. Ever.

There’s not really a function for this in the standard library.
I use something like:

split( std::string const& original, char separator )
    std::vector<std::string> results;
    std::string::const_iterator start = original.begin();
    std::string::const_iterator end = original.end();
    std::string::const_iterator next = std::find( start, end, separator );
    while ( next != end ) {
        results.push_back( std::string( start, next ) );
        start = next + 1;
        next = std::find( start, end, separator );
    results.push_back( std::string( start, next ) );
    return results;

I believe Boost has a number of such functions. (I implemented
most of mine long before there was Boost.)

Browse More Popular Posts

Leave a Comment