How to convert string to integer in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to convert string to integer in c++

string number_str = "1"; and i want to convert the number_str to integer

26th Oct 2017, 12:19 PM
Tanfirul Roibafi Muhammad
Tanfirul Roibafi Muhammad - avatar
2 Answers
+ 4
You may use this Function, convert: #include <sstream> int convert(string d) { int var; stringstream ss(d); ss>>var; return var; } int main() { cout<<convert("1"); } Also, there are standard string conversion functions stoi, stol, stof etc for conversion of string to int, long and float respectively. You may use those if you don't wish to define your own function.
26th Oct 2017, 12:45 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
26th Oct 2017, 12:48 PM
Andrew
Andrew - avatar