现在的位置: 首页 > 综合 > 正文

C++中stringstream的用法

2019年04月07日 ⁄ 综合 ⁄ 共 304字 ⁄ 字号 评论关闭

详细的介绍,传送门:http://www.cppblog.com/Sandywin/archive/2007/07/13/27984.html

它支持string->int、 int->string,还可以int->char,在某些方面用起来比sprintf更加安全、可靠,但在大数据量面前,消耗的时间比较长。

先给出一些示例代码:

int next(int n, int k)
{
	stringstream ss;
	ss << (LL) k*k;
	string s = ss.str();
	if(s.length() > n) s = s.substr(0, n);
	int ans;
	stringstream ss2(s);
	ss2 >> ans;
	return ans;
}

【上篇】
【下篇】

抱歉!评论已关闭.