split string into container on char; efficiently? Here is my single parse code for splitting a std::string on a char into a std::vector&lt;std::string&gt;:

#include &lt;iostream&gt;
#include &lt;vector&gt;

std::vector&lt;std::string&gt; split_uri(std::string, unsigned chr='/');

int main() {
    std::vector&lt;std::string&gt; uri_v = split_uri(uri, '/');

    for (auto elem: uri_v)
         std::cout &lt;&lt; elem &lt;&lt; std::endl;
}

std::vector&lt;std::string&gt; split_uri(std::string uri, unsigned chr) {
    bool start=true;
    std::string part;
    std::vector&lt;std::string&gt; vec;
    vec.reserve(uri.length()/2);

    for(char c: uri) {
        switch(c) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                part += c;
                break;
            default:
                if (c == chr) {
                    if (!start)
                        vec.push_back(part);
                    else start = false;
                    part.clear();
                    part += c;
                }
                else {
                    std::cerr &lt;&lt; "Invalid URI; from: \'" &lt;&lt; c &lt;&lt; "\' (hex: " &lt;&lt; std::hex &lt;&lt; "0x" &lt;&lt; (unsigned int)c &lt;&lt; ")\n";
                    vec.clear();
                    return vec;
                }
        }
    }

    return vec;
}


What can I do to improve its efficiency?
First, consider passing potentially expensive-to-copy objects such as std::string as const reference rather than by-value. The obvious approach is to use std::istringstream and std::getline.

std::vector&lt;std::string&gt; split_uri(const std::string uri&amp;, char chr)
{
    std::istringstream iss(uri);
    std::vector&lt;std::string&gt; vec;

    for(std::string token; getline(iss, token, chr); )
        vec.push_back(token);

    return vec;
}


With the input being \bar\can, the output is {"", "bar", "can"}.

However, std::istringstream is not known for its speed. If you don't want to use std::istringstream, you don't have to. The following should be much faster:

std::vector&lt;std::string&gt; split_uri(const std::string&amp; uri, char chr)
{
    std::string::const_iterator first = uri.cbegin();
    std::string::const_iterator second = std::find(first+1, uri.cend(), chr);
    std::vector&lt;std::string&gt; vec;

    while(second != uri.cend())
    {
        vec.emplace_back(first, second);
        first = second;
        second = std::find(second+1, uri.cend(), chr);
    }

    vec.emplace_back(first, uri.cend());

    return vec;
}


With the input being \bar\can, the output is {"\bar", "\can"}.