how to convet string “1234” to integer 1234 and vice versa?
Coversion problem
Hey Amit, there are many ways to convert string to integer and vice versa in c++.
- String to integer
-
using stringstream class
string str = “1234”;
stringstream st(str);
int num = 0;
st >> num; -
using stoi()
string str = "1234"; int num = stoi(str);
- integer to string
-
using itoa()
int num = 1234; char *intStr = itoa(num); string str = string(intStr);
-
auto s = std::to_string(1234);
If you want to explore more about these functions you can refer this.