標題:
int 與 string 的相互轉換
[打印本頁]
作者:
陳品肇
時間:
2019-7-20 12:55
標題:
int 與 string 的相互轉換
試利用 stringstream 型別, 來進行 int 與 string 的相互轉換.
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a<<endl;
ss<<a;
ss>>str;
cout<<"字串型態: "<<str<<endl;
system("pause");
return 0;
}
複製代碼
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a+456<<endl;
ss<<a;
ss>>str;
cout<<"字串型態: "<<str+"456"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
吳孟書
時間:
2019-7-20 14:21
#include<iostream>
#include<cstdlib>
#include<sstream>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態:"<<a<<endl;
ss<<a;
ss>>str;
cout<<"字串型態:"<<str<<endl;
system("pause");
return 0;
}
複製代碼
#include<iostream>
#include<cstdlib>
#include<sstream>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態:"<<a+456<<endl;
ss<<a;
ss>>str;
cout<<"字串型態:"<<str+"456"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
曲書辰
時間:
2019-7-20 14:21
本帖最後由 曲書辰 於 2019-7-20 14:25 編輯
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream a;
string b;
int c=123;
a<<c;
a>>b;
cout<<b<<endl;
system("pause");
return 0;
}
複製代碼
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream a;
string b;
int c=123;
a<<c;
a>>b;
cout<<b+"456"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
吳孟修
時間:
2019-7-20 14:23
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a+456<<endl;
ss<<a;
ss>>str;
cout<<"字串型態: "<<str+"456"<<endl;
system("pause");
return 0;
}
複製代碼
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a<<endl;
ss<<a;
ss>>str;
cout<<"字串型態: "<<str<<endl;
system("pause");
return 0;
}
複製代碼
作者:
王瑞喻
時間:
2019-7-20 14:24
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a<<endl;
ss<<a;
ss>>str;
cout<<"字串形態: "<<str<<endl;
system("pause");
return 0;
}
複製代碼
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a+456<<endl;
ss<<a;
ss>>str;
cout<<"字串形態: "<<str+"456"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
田宇任
時間:
2019-7-20 14:26
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int king=123;
string str;
cout<<"整數型態"<<king<<endl;
ss<<king;
ss>>str;
cout<<"字串型態: "<<str<<endl;
system("pause");
return 0;
}
複製代碼
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int king=456;
string str;
cout<<"整數型態: "<<king+789<<endl;
ss<<king;
ss>>str;
cout<<"字串型態: "<<str+"789"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
洪寬瀧
時間:
2019-7-20 14:27
#include<iostream>
#include<cstdlib>
#include<sstream>
using namespace std;
int main()
{
stringstream ss;
int u=44;
string str;
cout<<"整數型態: "<<u<<endl;
ss<<u;
ss>>str;
cout<<"字串型態: "<<str<<endl;
//string str2="狗咬豬";
//string str;
//ss<<str1<<str2<<"............";
//ss>>str;
//cout<<str<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<cstdlib>
#include<sstream>
using namespace std;
int main()
{
stringstream ss;
int u=44;
string str;
cout<<"整數型態: "<<u+456<<endl;
ss<<u;
ss>>str;
cout<<"字串型態: "<<str+"456"<<endl;
//string str2="狗咬豬";
//string str;
//ss<<str1<<str2<<"............";
//ss>>str;
//cout<<str<<endl;
system("pause");
return 0;
}
複製代碼
作者:
湯郡一
時間:
2019-7-20 14:30
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a<<endl;
ss<<a;
ss>>str;
cout<<"字串型態: "<<str<<endl;
system("pause");
return 0;
}
複製代碼
#include<iostream>
#include<sstream>
#include<cstdlib>
using namespace std;
int main()
{
stringstream ss;
int a=123;
string str;
cout<<"整數型態: "<<a+456<<endl;
ss<<a;
ss>>str;
cout<<"字串型態: "<<str+"456"<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2