C++, stoi() function. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

21st Feb 2018, 8:39 PM
CuriousCI
CuriousCI - avatar
3 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?
21st Feb 2018, 8:52 PM
Chris
Chris - avatar
+ 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; }
22nd Feb 2018, 4:50 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
strtol(); is included in <string>.
24th Feb 2018, 4:35 PM
CuriousCI
CuriousCI - avatar