I have two cases of input for which I want to use the same method. The first case is that the given parameter is a std::string containing three digits that I need to convert into int:
std::string pointLine = "1 1 1";
The second case is that the given parameter is a std::string containing three "not yet doubles" that I need to convert into doubles:
std::string pointLine = "1.23 23.456 3.4567"
I have written the following method:
std::vector<double> getVertexIndices(std::string pointLine) {
vector<int> vertVec;
vertVec.push_back((int) pointLine.at(0));
vertVec.push_back((int) pointLine.at(2));
vertVec.push_back((int) pointLine.at(4));
return vertVec;
}
This works fine for the first case, but not for having a line that is supposed to be converted to doubles.
So I tried the solution Double split in C . I get that my delimiter would be " ".
This is what I came up with as for now, but the program crashes after first call of the following method:
std::vector<double> getVertexIndices(std::string pointLine) {
vector<double> vertVec;
char * result = std::strtok(const_cast<char*>(pointLine.c_str()), " ");
while(result != NULL ) {
double vert = atof (result);
vertVec.push_back(vert);
char * result = std::strtok(NULL, " ");
}
return vertVec;
}