istringstream 和 ostringstream 的使用例子

本文通过实例演示了如何使用istringstream和ostringstream进行字符串的读写操作。通过istringstream将字符串拆分为单词,并利用ostringstream重新构建字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 此文版权属于作者本人,任何人、媒体或者网站转载、借用都必须征得作者本人同意

sstream_example.cpp

00001   //  $Id$
00002   /* *
00003 * @file     sstream_example.cpp
00004 * @brief    istringstream 和 ostringstream 使用例子
00005 *
00006 * 版权所有© 2004~2005
00007 * All rights reserved. 
00008 *
00009 * @version  $Revision$
00010 * @author   智勇
00011 * @date     2006年3月9日 21:12:11
00012 *
*/
00013  #include  < iostream >
00014  #include  < sstream >
00015  
00016  using namespace std;
00017  
00018   void  sstream_example(){
00019  
00020      cout  <<   " ************************************************* "   <<  endl;
00021      cout  <<   " istringstream and ostringstream example: "   <<  endl  <<  endl;
00022  
00023      istringstream istr( " SchemaSpy analyzes schema metadata.  "
00024           " SchemaSpy also identifies several common schema anomalies.     " );
00025  
00026      ostringstream ostr;
00027  
00028      cout  <<   " Original string is :  "   <<  endl
00029           <<  istr.str()  <<  endl;
00030      cout  <<   " ------------------------------------------------- "   <<  endl;
00031  
00032      string str;
00033  
00034      cout  <<   " [ " ;
00035       while ( ! istr.eof()){
00036          istr  >>  str;
00037           if (istr)
00038          {
00039              cout  <<  str  <<   "   " ;
00040              ostr  <<  str  <<   "   " ;
00041          }
00042      };
00043      cout  <<   " ] " ;
00044      cout  <<  endl;
00045      cout  <<   " ------------------------------------------------- "   <<  endl;
00046      cout  <<   " [ " ;
00047      cout  <<  ostr.str();
00048      cout  <<   " ] " ;
00049      cout  <<  endl;
00050  }
00051  
00052   int  
00053  main( int  argc,  char *  argv[])
00054  {
00055      sstream_example();
00056           return   0 ;
00057  }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值