prettify

Jan 28, 2016

C++ string::split

I can understand the reason why C does not provide the string split function, but C++ is another story. A guy told me today C++ is going to add an split function, but not in C++11. It is been so late. I always have to write something like this

#include <sstream>
#include <vector>

std::vector<std::string> split(const std::string& s, const char delim){
    std::vector<std::string> res;
    std::string item;
    std:stringstream ss(s);
    while(std::getline(ss, item, delim)) {
        if(!item.empty()) res.push_back(item);
    }
    return res;
}

that's too much job from Java's split, no mention the powerful regex based split in Perl.

No comments:

Post a Comment