Reputation: 345
We have a string which consists of "RESERVE number1,number2,number3,number4"
, where number1,number2,number3 and number4 can be any number from 0 to 10, without decimal point.
The string's format cannot be changed, and we want to extract each of those numbers into their respective int variables.
We have tried, so far, to:
Use substr, alongside find, but in order to use find again, we may have to remove the "matched part" of the string from the original one, and we are not sure on how to do that
Use stoi, but then we have the same problem as before. Most examples we have seen make use of stoi, but always in cases where there is only one numeric value in the string, not multiple ones.
Any ideas are welcome!
Upvotes: 0
Views: 164
Reputation: 250
Use sscanf. Example:
int num1, num2, num3, num4;
std::string s = "RESERVE 11,22,33,44";
sscanf(s.c_str(), "RESERVE %d,%d,%d,%d", &num1, &num2, &num3, &num4);
Upvotes: 1