首页 > 编程语言 > 详细

C++各种格式转换

时间:2020-07-08 19:23:40      阅读:82      评论:0      收藏:0      [点我收藏+]

字符转换类型 stringstream

头文件 sstream


#include<bits/stdc++.h>

using namespace std;

int main() {
stringstream ss;
//--------int转string-----------
int a = 100;
string str;
ss << a;
ss >> str;
cout << str << endl;
/*如果你想通过使用同一stringstream对象实现多种类型的转换,
   注意在每一次转换之后都必须调用clear()成员函数。*/
ss.clear();
//--------string转char[]--------
string name = "colinguan";
char cname[200];
ss << name;
ss >> cname;
cout << cname << endl;
ss.clear();
//--------string转double/int--------
double dou;
int intt;
// string -> double
str = "123.456789";
ss << str;
ss >> dou;
cout << "dou: " << dou << endl;
ss.clear();
// string -> int
str = "654321";
ss << str;
ss >> intt;
cout << "intt: " << intt << endl;
ss.clear();
}

技术分享图片

 

C++各种格式转换

原文:https://www.cnblogs.com/HDawn/p/13268248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!