C++ stringstream的用法:用stringstream来实现任意类型之间的转换,用来巧妙解竞赛问题。 ————墨白

饱受Csprintf和sscanf()摧残的我们,在学习了C++的stringstream之后,忍不住激动地大叫一声“太爽了!”
通过stringstream 创造的流,我们可以轻松的实现各种类型之间的转换!
废话不多说,让我们一起来进入C++stringstream的学习中吧!

stringstream的用法总结

注意:在讲解stringstream的时候,有一个要点广大读者一定要注意!!!stringstream声明的流对象一定一定只能放在流赋值表达式的左边。比如:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
    stringstream stream;
    int k=41232;
    string str1;
    //k>>stream;//错误! stream必须放在左边!
    stream<<k;//correct
    stream>>str1;//correct
    //str1<<stream;//错误! stream必须放在左边!!
    cout<<str1<<endl;
    return 0;
}

好了,下面我们开始正式讲解stringstream用法,首先,我们知道,stringstream集istringstream和ostringstream的功能于一身,所以我们这里只讲stringstream,要想使用stringstream,必须包含头文件
sstream !

#include<sstream>

下面我们通过一些实例来初步理解stringstream用法

1.int类型转string类

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
    int i=23412;
    string str1("hhhhhh");
    stringstream stream;
    stream<<i;//把i的值给stream
    stream>>str1;//把stream的值给str1
    cout<<str1<<endl;
    return 0;
}

2.就连字符数组也可以和string类相互转换

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
    char a[100]="hhhhhhhhh";
    double k=3.1423;
    stringstream stream;
    stream<<k;
    stream>>a;
    cout<<a<<endl;
    return 0;
}

3.如果流中是有空格的数,那么流要如何把值付给别的变量呢?

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
    string str1("wo hao shuai a a a a ");
    string a1,a2,a3,a4;
    stringstream stream(str1);
    stream>>a1;
    stream>>a2;
    stream>>a3;
    stream>>a4;
    cout<<a1<<endl;//输出wo
    cout<<a2<<endl;//输出hao
    cout<<a3<<endl;//输出shuai
    cout<<a4<<endl;//输出a
    return 0;
}

由此可见,流中的数据以空格分割开,当给别人传输的时候,一次只穿空格之前的所有数据,并且依次向下。

4.重复用到同一个流时,千万要学会用流的.clear()

#include<iostream>
#include<sstream>
using namespace std;
int main()
{
    int first(111),second(222);
    stringstream stream;
    stream<<first;
    stream>>second;
    cout<<second<<endl;//输出111
    stream<<2345;
    stream>>second;
    stream>>k;
    cout<<second<<endl;//还是111
    return 0;
}

最可怕的其实是这个 如果不用clear(),那么当你再次往流里赋一个值,你猜猜会发生什么?
答案就是,什么也不会发生。你再次用stream给别的变量赋值,他还是会赋原来的那个值。

#include<iostream>
#include<sstream>
using namespace std;
int main()
{
    int first(111),second(222);
    stringstream stream;
    stream<<first;
    stream>>second;
    cout<<second<<endl;//输出111
    stream.clear();
    stream<<2345;
    stream>>second;
    cout<<second<<endl;//2345,correct!
    return 0;
}

来一道例题喽

排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6476 Accepted Submission(s): 1865

Problem Description
输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。

你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。

Input
输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。

Output
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。

Sample Input

0051231232050775

Sample Output

0 77 12312320

#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
using namespace std;
int a[10010];
int main()
{
	string str;
	stringstream stream;
	int x,len;
	while(cin>>str){
		for(int i=0;i<str.size();i++)
			if(str[i]=='5') str[i]=' ';
		stream.clear();
		stream<<str;
		x=0;
		while(stream>>a[x])
			x++;
		len=x;
		sort(a,a+len);
		for(int i=0;i<len;i++)
			cout<<a[i]<<" ";
	}
	return 0;
}
  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值