+ 3
C++, stoi() function.
SoloLearn's compiler says "stoi was not declared in this scope". I already tryed using std::stoi, and it says that there isn't stoi in std. How could I use stoi?
4 Answers
+ 7
stoi is included in the string library. However, it doesn't seem to work in CodePlayground. stoi was introduced in C++11, perhaps SoloLearn uses an older standard?
+ 3
You may define your own stoi function using strtol from <cstdlib>.
Eg :
int stoi (string s, size_t* idx, int base = 10)
{
char* ptr; int num = strtol
(s.c_str(),&ptr,base); if(idx!=nullptr)
*idx = s.find(ptr); return num;
}
Or if you just want to convert string to integer without any base conversions, you may try stringstream.
int stoi(string s)
{
stringstream ss(s); int num;
ss>>num; return num;
}
0
strtol(); is included in <string>.