1#include <iostream>
2#include <string>
3
4int main() {
5
6 std::string str = "123";
7 int num;
8
9 // using stoi() to store the value of str1 to x
10 num = std::stoi(str);
11
12 std::cout << num;
13
14 return 0;
15}
1// For C++11 and later versions
2string str1 = "45";
3string str2 = "3.14159";
4string str3 = "31337 geek";
5
6int myint1 = stoi(str1);
7int myint2 = stoi(str2);
8int myint3 = stoi(str3);
9
10// Output
11stoi("45") is 45
12stoi("3.14159") is 3
13stoi("31337 geek") is 31337
1#include<string>
2string str1 = "45";
3string str2 = "3.14159";
4string str3 = "31337 geek";
5
6int myint1 = stoi(str1);
7std::cout<<stoi(str1);
1// EXAMPLE
2std::string sStringAsString = "789";
3int iStringAsInt = atoi( sStringAsString.c_str() );
4
5/* SYNTAX
6atoi( <your-string>.c_str() )
7*/
8
9/* HEADERS
10#include <cstring>
11#include <string>
12*/
1#include <iostream>
2#include <sstream>
3
4using namespace std;
5
6int main() {
7 string s = "999";
8
9 stringstream degree(s);
10
11 int x = 0;
12 degree >> x;
13
14 cout << "Value of x: " << x;
15}