字符串加集合应用,注意读数据的结束判断和用scanf不同,应该是gets(buffer)!=NULL这样。
#include <stdio.h>
#include <set>
#include <algorithm>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
char buffer[250];
set<string> dict;
void split()
{
int i, len, count;
char temp[100];
string str;
len = strlen(buffer);
count = 0;
for(i=0; i<=len; i++)
{
if(buffer[i]>='a' && buffer[i]<='z')
temp[count++] = buffer[i];
else if(buffer[i]>='A' && buffer[i]<='Z')
temp[count++] = buffer[i] + ('a'-'A');
else
{
if(count) //防止空行
{
str.assign(temp, count);
dict.insert(str);
}
count = 0;
}
}
}
int main(void)
{
dict.clear();
while(gets(buffer) != NULL)
{
split();
/*
//这里是本机测试的时候用的
if(!strcmp(buffer, "over"))
break;
*/
}
set<string>::iterator it;
for(it=dict.begin(); it!=dict.end(); it++)
cout << *it << endl;
return 0;
}