Answers for "how to convert string to an integer in c++"

C++
11

string to number in c++

// For C++11 and later versions
string str1 = "45"; 
string str2 = "3.14159"; 
string str3 = "31337 geek"; 

int myint1 = stoi(str1); 
int myint2 = stoi(str2); 
int myint3 = stoi(str3); 

// Output
stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 geek") is 31337
Posted by: Guest on May-20-2020
0

string to int c++

#include <sstream>
using namespace std;
int main()
{
    stringstream string_to_int;
    string s1="12345";
    int n1;

    string_to_int<<s1;
    //也可以使用string_to_int.str(s1);
    //或者 s1=string_to_int.str();
    
    string_to_int>>n1;

    cout<<"s1="<<s1<<endl;//s1=12345
    cout<<"n1="<<n1<<endl;//n1=12345
    
}
Posted by: Guest on May-15-2022

Code answers related to "how to convert string to an integer in c++"

Browse Popular Code Answers by Language