Here is my single parse code for splitting a std::string on a char into a std::vector<std::string>:#include <iostream>#include <vector>std::vector<std::string> split_uri(std::string, unsigned chr='/');int main() {    std::vector<std::string> uri_v = split_uri(uri, '/');    for (auto elem: uri_v)         std::cout << elem << std::endl;}std::vector<std::string> split_uri(std::string uri, unsigned chr) {    bool start=true;    std::string part;    std::vector<std::string> 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 << Invalid URI; from: \' << c << \' (hex:  << std::hex << 0x << (unsigned int)c << )\n;                    vec.clear();                    return vec;                }        }    }    return vec;}What can I do to improve its efficiency?
split string into container on char; efficiently?
c++;performance;memory management;vectors;cyclomatic complexity
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<std::string> split_uri(const std::string uri&, char chr){    std::istringstream iss(uri);    std::vector<std::string> 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<std::string> split_uri(const std::string& uri, char chr){    std::string::const_iterator first = uri.cbegin();    std::string::const_iterator second = std::find(first+1, uri.cend(), chr);    std::vector<std::string> 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}.