例题5-3 UVa10815 Andy's First Dictionary(STL:set&&sstream的应用)
题意:
输入一长串字符串,将其中的单词按字典序输出。
要点:
STL中的set就像数学中的集合,每个元素最多只出现一次。ssteam可以很简单的将字符串转换为整数或反之。这题注意将所有不是字母的字符变成空格再进行一次出入流操作来除掉空格。
#include<iostream>
#include<set>
#include<string>
#include<sstream>
using namespace std;
set<string> dict;
int main()
{
string s, buf;
while (cin >> s)
{
for (int i = 0; i < s.length(); i++)
{
if (isalpha(s[i]))
s[i] = tolower(s[i]);
else
s[i] = ' ';//将不是字母的赋值为空格
}
stringstream ss;
ss << s;
while (ss >> buf)//这里通过入流进流使赋值为空格的字符被删除
dict.insert(buf);
}
for (set<string>::iterator it = dict.begin(); it != dict.end(); it++)//set<string>::iterator是迭代器的意思,可以看出it是一个指向string的指针
cout << *it << endl;
return 0;
}