力为的技术博客

联系 聚合 管理
  154 Posts :: 1 Stories :: 561 Comments :: 0 Trackbacks

今天在论坛里看到这样一个问题:
要求将字符串 "mallid=310103123456\ntermno=139654785231\norder=00003995\ngcode=0000130\npaydate=20051031\nname=\ncardno=5248000262355365\nlimit=0804\nprice=2210"\n为分界符,提取出所有的字符串。
刚好最近在看STL,感觉可以用stream来解决,于是就小试了一下。结果还真令人满意


#include 
<iostream>
#include 
<sstream>
#include 
<vector>
#include 
<algorithm>
#include 
<ITERATOR>

#define INPUT_STRING "mallid=310103123456\ntermno=139654785231\norder=00003995\ngcode=0000130\npaydate=20051031\nname=\ncardno=5248000262355365\nlimit=0804\nprice=2210"
    
typedef std::vector
<std::string> strvec;

int _tmain(int argc, _TCHAR* argv[])
{
    
using namespace std;

    istringstream iss(INPUT_STRING);
    strvec strList;
    
string strTemp;

    
while(iss >> strTemp)
    
{
        strList.push_back(strTemp);
    }

        
        
// out put the result
    copy(strList.begin(), strList.end(), ostream_iterator<string>(cout, "\n"));

    
return 0;
}



posted on 2005-11-03 10:14 力为 阅读(7597) 评论(14)  编辑 收藏 引用 所属分类: 4. C++ FAQ

评论

# re: 用istringstream 解决问题一则 2005-11-29 11:21 Squirrel
stringstream,我现在常用它来做字符串数组的存取。喜欢将它用作函数的输出参数,来得到字符串列表,不知道这样做是否合适,似乎用vector来做更好些。  回复  更多评论
  

# re: 用istringstream 解决问题一则 2005-12-01 22:42 力为
我感觉用vector好一些  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-03-02 09:35 周星星
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>

#define INPUT_STRING "mallid=310103123456\ntermno=139654785231\norder=00003995\ngcode=0000130\npaydate=20051031\nname=\ncardno=5248000262355365\nlimit=0804\nprice=2210"

typedef std::vector<std::string> strvec;

int main(int argc, char* argv[])
{
using namespace std;

istringstream iss( INPUT_STRING );
strvec strList;

// in
copy( istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(strList) );

// out
copy( strList.begin(), strList.end(), ostream_iterator<string>(cout,"\n") );

return 0;
}

  回复  更多评论
  

# 要来再少点儿? :P 2006-03-02 19:16 LeavesOfFloat
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

int main(int argc, char* argv[])
{
using namespace std;

const string str("mallid=310103123456\ntermno=139654785231\norder=00003995\ngcode=0000130\npaydate=20051031\nname=\ncardno=5248000262355365\nlimit=0804\nprice=2210")
vector<string> strList;

copy( istream_iterator<string>(istringstream(str)), istream_iterator<string>(), back_inserter(strList) );

copy( strList.begin(), strList.end(), ostream_iterator<string>(cout,"\n") );

return 0;
}
  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-03-04 10:00 力为
看来完美了:)  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-03-09 11:06 Squirrel
有这样一个关于stringstream的问题。
将一系列以string 和unsigned long 组成的序列放入流中,如何将它们还原出来?(string中允许有空格)

#include "stringstream"
#include "iostream"

using namespace std;

void main()
{
stringstream ss;

string item1;
unsigned long item2;

ss << "Hello World" <<endl << 80 <<endl
<< "Hi Jack" << endl << 90 <<endl
<< "Hello China" << endl << 100 <<endl;

while ( ss >> item1 >> item2 )
{
cout << item1 << "\t" << item2 <<endl;
}

}

上面的程序打印不出来任何东西,因为空格是作为流的分隔符的.
不知道大家有什么好的办法?  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-03-23 22:04 stone
用 strtok()也可以
#include <string.h>
#include <stdio.h>

char string[] = "mallid=310103123456\ntermno=139654785231\norder=00003995\n"
"gcode=0000130\npaydate=20051031\nname=\ncardno=5248000262355365\n"
"limit=0804\nprice=2210" ;

char seps[] = "\n";
char *token;

int _tmain(int argc, _TCHAR* argv[])
{
printf( "Tokens:\n" );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}

return 0;
}

  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-05-27 21:03 sybal
将一系列以string 和unsigned long 组成的序列放入流中,如何将它们还原出来?(string中允许有空格)
这个问题可以提出以下解答吗?  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-06-08 16:00 力为
to sybal:

当string中有空格后,那就意味着string中也可能出现数字,这样无法用流简单的还原。恐怕需要加入特殊的字符串结束符。
  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-06-23 14:20 子弹

istringstream 的方法不通用吧……

如若分隔符不是"\n",而是其他呢  回复  更多评论
  

# re: 用istringstream 解决问题一则 2006-06-23 14:23 子弹

strtok()第一个参数要求非const 的char*也是一种限制。  回复  更多评论
  

# re: 用istringstream 解决问题一则[未登录] 2007-03-26 00:56 大头
贴个史上最强版:

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
string input("mallid=310103123456\ntermno=139654785231\norder=00003995\ngcode=0000130\npaydate=20051031\nname=\ncardno=5248000262355365\nlimit=0804\nprice=2210" );
istringstream iss(input);

// out
copy( istream_iterator<string>(iss) , istream_iterator<string>(), ostream_iterator<string>(cout,"\n") );

system("pause");
return 0;
}
  回复  更多评论
  

# re: 用istringstream 解决问题一则[未登录] 2007-03-27 22:08 大头
上个仍然不够强。主要是dev出现问题,在vs2005下通过只要一行代码

copy(istream_iterator<string>(istringstream("zhangwujian\nzhangyue")) , istream_iterator<string>(), ostream_iterator<string>(cout,"\n") );

不可能比这更强了吧  回复  更多评论
  

# re: 用istringstream 解决问题一则 2007-04-16 16:50 aslucky
@LeavesOfFloat
str少了个分号,呵呵。  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理