首页 > 其他 > 详细

Stringstream

时间:2020-05-11 09:21:29      阅读:48      评论:0      收藏:0      [点我收藏+]

Stringstream

  • \(stringsteam\)用于进行数据类型转换,\(<sstream>\)库定义了三种类:\(istringstream\)\(ostringstream\)\(stringstream\),分别用来进行流的输入、输出和输入输出操作。
  • 接下来举一个栗子,通过这道题我们来介绍下从\(int\)转化为\(string\)的过程

Digits Sequence (Easy Edition)

  • 这道题是一道很水的题,我们通过这道题来介绍下\(streamstring\)
  • 我们可以考虑把每一个字符都压入\(s\),最后输出第\(n-1\)项即可
  • 总体来说,\(streamstring\)\(string\)的使用极为相似
    • \(stringstream\):\(s.str().size()\)(取长),\(sort(a.s.str().begin(),a.s.str().begin())\)(排序)。
    • \(string\):取长排序就不必多说了
  • 有没有发现区别就是中间多了个\(.str()\)
  • 接下来就是一点也不激动人心的代码了:
#include<bits/stdc++.h>//Forever_chen
#define RT register
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c==‘-‘,c=getchar();
	while(isdigit(c))x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f)x=-x;return x;
}
template<class t>inline void write(t x){
	if(x<0)putchar(‘-‘),write(-x);
	else{if(x>9)write(x/10);putchar(‘0‘+x%10);}
}
template<class t>inline void writeln(t x){
	write(x);putchar(‘\n‘);
	return;
}
template<class t>inline void write_blank(t x){
	write(x);putchar(‘ ‘);
	return;
}
int n,k;
stringstream s; //定义一个stringstream类型的s
signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	read(n);
	for(int i=1;s.str().size()<=n;i++){//取长,和string类型相类似
		s<<i;//将int类型的i压入s
	}
	cout<<s.str()[n-1];//输出第n-1项
	//system("pause");
	return 0;
}

Stringstream

原文:https://www.cnblogs.com/Forever-chen/p/12866971.html

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