C++:split文字列分割関数
ACPP由来文字列分割関数
ddd
バージョン2
vector<string> split(const std::string &s)
{
vector<string> vec_ret;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size()){
// ignore the space
while (i != s.size() && isspace(s[i]))
++i;
string_size j = i;
// get all the char
while (j != s.size() && !isspace(s[j]))
++j;
if (i != j){
vec_ret.push_back(s.substr(i, j-i));
i = j;
}
} // end of while
return vec_ret;
}
ddd
バージョン2
//
vector<string> split(const string &s)
{
vector<string> vec_ret;
typedef string::const_iterator iter;
iter i = s.begin();
while (i != s.end()) {
i = find_if(i, s.end(), not_space);
iter j = find_if(i, s.end(), space);
// get valid word
if (i != j) {
vec_ret.push_back(string(i, j));
i = j;
}
}
return vec_ret;
}