- int len = strlen(param);
- char tmpbuf[len + 1];
- char *tmp = tmpbuf;
- memcpy(tmp, param, len + 1);
- degree = std::count(tmp, tmp + len, ',');
- factor = new double[degree + 1];
- for (int i = degree ; i > 0 ; i--) {
- char *next = strchr(tmp, ',');
- *next++ = '\0';
- factor[i] = atof(tmp);
- tmp = next;
+ std::istringstream paramstream(param);
+ std::string token;
+ while (std::getline(paramstream, token, ',')) {
+ std::istringstream str(token);
+ double f;
+ if ((str >> f) && str.eof())
+ factors.push_back(f);
+ else
+ throw std::invalid_argument("cost_func(): "
+ "cannot parse \"" + token + "\"");