I am trying to split a string like "8573656.5465467.5435325.3253255" into separate int variables each containing the group of numbers between the dots. Someone told me this would work but I have no clue what it is. Could someone explain it maybe or give a better methord.
1 2 3 4 5 6 7 8 9 10
istringstream ins(YourStringName);
std::vector<long> vectorLong;
char dot; // A character to retrieve the '.'.
long temp;
while(ins >> temp) {
ins >> dot; // Retrieve the dot and throw it away.
vectorLong.push_back(temp); // Store the number in the vector. }
What part is it that you have problem with? std::istringstream is a stream class that can be used to extract data from a file. You can use it the same way as other input streams like std::ifstream and std::cin. YourStringName is the string with the numbers and the dots. ins >> temp reads the next integer value and stores it into temp. The loop will continue loop until it fails to read an integer value. ins >> dot; reads the next char that is not a whitespace char and stores it in dot. The code doesn't check if dot == '.' so any other char that is not a digit and not a whitespace will work as a separator. vectorLong.push_back(temp); adds the value of temp to the end of vectorLong.
getline takes an optional delimiter to tokenize a stream. Just feed the string into a stringstream first. Once again, there are many ways but this one is pretty clear and it is standard.