Stringstream

Stringstream

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

Digits Sequence (Easy Edition)

  • 这道题是一道很水的题,我们通过这道题来介绍下\(streamstring\)
  • 我们可以考虑把每一个字符都压入\(s\),最后输出第\(n-1\)项即可
  • 接下来就是一点也不激动人心的代码了:
#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;
}

上一篇:1010 一元多项式求导 (25 分)


下一篇:p1554 梦中的统计