[C++] Convert between string and int

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{ 
 stringstream ss;
 string s="456";
 int x;
 
 //string to int(or double etc...)
 ss<<s;
 ss>>x;
 cout<<x;

 //string to int using stoi 
 x = stoi(s);
 //clean up stringstream
 
 ss.str("");
 ss.clear();
 
 //int(or double etc...) to string
 x=100;
 s=to_string(x);
 cout<<s;
 
 return 0;
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *