I want to extract some tokens from a std::string by delimiter, and write a function as follows, but it still has errors:
enum TO_TYPE { TO_INT=0, TO_FLOAT, TO_STRING};
template<typename T>
vector<T> string_to_token(string &str, const char* delim, TO_TYPE to_type)
{
char *p=new char[str.size()];
memcpy(p,str.c_str(),str.size());
switch(to_type)
{
case TO_INT:
{
vector<int> res;
char *token;
char *state;
for (token = strtok_r(p, delim, &state);
token != NULL;
token = strtok_r(NULL, delim, &state))
{
res.push_back(atoi(token));
}
delete[] p;
return res;
}
break;
case TO_FLOAT:
{
vector<float> res;
char *token;
char *state;
for (token = strtok_r(p, delim, &state);
token != NULL;
token = strtok_r(NULL, delim, &state))
{
res.push_back(atof(token));
}
delete[] p;
return res;
}
break;
case TO_STRING:
{
vector<string> res;
char *token;
char *state;
for (token = strtok_r(p, delim, &state);
token != NULL;
token = strtok_r(NULL, delim, &state))
{
res.push_back(string(token));
}
delete[] p;
return res;
}
break;
}
}
Usage:
string str="lab,yuv,hsv";
vector<string> colorspace=string_to_token<string>(str,",");
It has errors in the line return res
.
I have tried to use void (*change_func)(char *temp_str)
as the callback function, but I don't know how to implement it.
If you were convenient, could you give me some advice, please? Thanks a lot.