I am new to C ++ (coming from C # background) and trying to learn how to convert a string to int.
I worked using stringstream and outputting it in double , for example:
const char* inputIndex = "5+2"; double number = 0; stringstream ss(inputIndex); ss >> number;
This works great. The problem I am facing is that the lines that I am processing start with a number, but may have other digits after the digits (for example, "5 + 2", "9- (3 + 2)" etc.)., stringstream parses the numbers at the beginning and stops when it encounters a non-digit as I need it.
The problem arises when I want to know how many characters were used for analysis in number. For example, if I parse 25+2 , I want to know that two characters were used for parsing 25 so that I can advance the line pointer.
So far I have been working by clearing the stringstream , inserting the processed number into it and reading the length of the resulting string:
ss.str(""); ss << number; inputIndex += ss.str().length();
While this works, it seems to me that it is really harmful (although it can only be because I come from something like C #), and I have a feeling that can cause a memory leak, because str() creates a copy of the string.
Is there any other way to do this, or should I stick with what I have?
Thanks.
user153498
source share