首先讲stream流类

头文件#include <sstream>

其功能是进行不同类型之间的转换,跟C语言 fscanf 和 fprintf 作用是同样的,可是比C语言的更简便更好用。

使用方法非常easy

#include <string>
#include <sstream>
#include <iostream> int main()
{
std::stringstream stream;
std::string result;
int i = 1000;
stream << i; //将int输入流
stream >> result; //从stream中抽取前面插入的int值
std::cout << result << std::endl; // print the string "1000"
}

上面代码是将string型的转换为int型。

!!注意:其在转换过程中有缓冲功能,比方有一个字符串“dada fffff”

通过while循环一次缓冲进入字符串中

比方:

        stringstream ss(s);
while(ss>>tmp)//能够按空格一个一个输出
val.insert(tmp);

以下将set容器。是一个树。能够进行判重,把反复的过滤掉

看这个题目:uva1085 Andy's First Diction

题意就是给你一串英文。让你把里面的单词读出来,不能反复。

用一个set容器,非常easy

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctype.h>
#include <set>
using namespace std; set<string> val;
int main()
{
string s,tmp;
while(cin>>s)
{
if(s=="break")
break;
for(int i=0;i<s.size();i++)
{
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s[i]=' ';
}
stringstream ss(s);
while(ss>>tmp)//能够按空格一个一个输出
val.insert(tmp);
}
for(set<string>::iterator it=val.begin();it!=val.end();it++)
cout<<*it<<endl;
return 0;
}

05-02 22:14