<sstream>中的 stringsstream 使用说明

来源:互联网 发布:java质数判断算法 编辑:程序博客网 时间:2024/06/01 21:34

stringstream是字符串流。它将流与存储在内存中的string对象绑定起来。

在多种数据类型之间实现自动格式化。

今天做Uva10815的一题 叫做 Andy’s First Dictionary用到的这个函数 然后觉得这个很强大

因为我也没有很透彻的弄明白stringstream的工作原理 只能在这里说一下 我用到的部分

利用stringstream可以将一段字符串中的单词提取出来

比如我有这样的一段英文:

Adventures in DisneylandTwo blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland Left."So they went home.

现在让你把里面的不同的单词输出来 这里就用到了stringstream

代码实现:

    string buf;    string s;    while(cin>>s){        stringstream ss(s);        while(ss>>buf){           cout <<buf<<endl;        }    }

选取了stringstream的那一部分 应该可以理解吧
这是一个模板类型的
用这个之前要加上sstream的头文件
我觉得这个对我以后的竞赛应该会有帮助 所以先整理一下学到的用法 虽然很不专业
但是可以很快的上手用 然后用的过程中慢慢去体会原理。