+ 11
can you give me a clear explaination of the function stoi()? And what does nullptr mean?
4 Answers
+ 21
stoi() has been labelled as unreliable and inaccurate given certain inputs according to Stack Overflow. Obtaining your desired values should be done via other means.
Be sure to declare the variable that will store the string to be converted as an int.
string simplestring = "a string";
int strToInt = stoi(simplestring.c_str());
It also helps to remember the purpose of stoi() by separating the letters like this: s - to - i ... to stand for "string to integer".
But to be honest, I find that problems are less occurant if you're to instead modify the string by assigning it to an int as a typecasted int, while at the same time using the ASCII table to convert it.
For example...
int typeCastedStr = 0;
string strToInt = "abcdef";
for (int x = 0; x < strToInt.length(); x++)
typeCastedStr += (int)strToInt.at(x) - 87;
I do this a few times in one of my programs. It's especially helpful when converting between hexadecimal, decimal, and vice-versa. This would be a good time to introduce "stringstream", but I'll leave that for you to look into in your own time.
+ 11
stoi() or string to integer is a function that conversation a string to an integer value and returns that value. A nullptr or Null pointer is just a pointer which has been set to point to NULL or nothing.
+ 10
thenks :D