<sstream>输入流.输出流

stringsteam 以空格为分隔,比如输入abc cbb输出就是先输出abc再输出cbb

#include<iostream>
#include<algorithm>
#include<sstream>
#include<string>

using namespace std;

int main()
{
	string line;
	while(getline(cin,line))
	{
		int sum=0;
		string x;
		stringstream ss(line);
		while(ss>>x)
			cout<<x<<endl;

	}
	return 0;
}

/*基本数据类型变字符串*/  
#include <fstream>   
#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    /*整型变字符串*/  
    int n = 10;  
    string str;  
    stringstream stream;  
      
    stream << n;  
    stream >> str;  
      
    cout<<str<<endl;  
    stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str("");否则下面输出10   
  
    /*char* 变 string*/  
    char cStr[10] = "china";  
      
    stream << cStr;  
    stream >> str;  
  
    cout<<str<<endl;  
    return 1;  
}  

/*字符串变基本数据类型*/  
#include <fstream>   
#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    /*字符串 变 double*/  
    double n;  
    string str = "12.5";  
    stringstream stream;  
      
    stream << str;  
    stream >> n;  
      
    cout<<n<<endl;  
    stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str("");   
  
    /*string 变 char* */  
    string str1 = "china";  
    char cStr[10];  
      
    stream << str1;  
    stream >> cStr;  
  
    cout<<cStr<<endl;//输出china   
    system("pause");  
    return 1;  
}  


istringsteam

#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    istringstream istr("1 56.7");  
  
    cout<<istr.str()<<endl;//直接输出字符串的数据 "1 56.7"   
      
    string str = istr.str();//函数str()返回一个字符串   
    cout<<str<<endl;  
      
    int n;  
    double d;  
  
    //以空格为界,把istringstream中数据取出,应进行类型转换   
    istr>>n;//第一个数为整型数据,输出1   
    istr>>d;//第二个数位浮点数,输出56.7   
  
    //假设换下存储类型   
    istr>>d;//istringstream第一个数要自动变成浮点型,输出仍为1   
    istr>>n;//istringstream第二个数要自动变成整型,有数字的阶段,输出为56   
  
    //测试输出   
    cout<<d<<endl;  
    cout<<n<<endl;   
    return 1;  
}  
ostringsteam
#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    //初始化输出字符串流ostr   
    ostringstream ostr("1234");  
    cout<<ostr.str()<<endl;//输出1234   
      
    ostr.put('5');//字符4顶替了1的位置   
    cout<<ostr.str()<<endl;//输出5234   
  
    ostr<<"67";//字符串67替代了23的位置,输出5674   
    string str = ostr.str();  
    cout<<str<<endl;  
    return 1;  
}  
clear()的注意事项

#include <iostream>   
#include <sstream>   
using namespace std;  
  
int main(int argc,char *argv[])  
{  
    std::stringstream stream;  
    string str;  
    while(1)  
    {     
        //clear(),这个名字让很多人想当然地认为它会清除流的内容。   
        //实际上,它并不清空任何内容,它只是重置了流的状态标志而已!   
        stream.clear();    
  
        // 去掉下面这行注释,清空stringstream的缓冲,每次循环内存消耗将不再增加!   
        //stream.str("");         
  
        stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs";  
        stream>>str;     
  
        //测试输出每次循环,你的内存消耗增加了多少!   
        cout<<"Size of stream = "<<stream.str().length()<<endl;  
        system("PAUSE");  
    }  
  
    system("PAUSE");  
    return EXIT_SUCCESS;  
}  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值