集合:和离散数学上的集合一样,就是只出现一次,<set>里的元素已经从小到大排好了
#include<iostream>
#include<string>
#include<set>
#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] = ' ';//tolower是把大写换小写
stringstream ss(s);//把普通的字符串转换成可读入set的流
while(ss >> buf) dict.insert(buf);//这两部把更改后的字符串读入集合
}
//*****************************************************************
for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
//*****************************************************************
//迭代器,便利输出,效果和数组一样
cout << *it << "\n";
return 0;
}
本文介绍了一种使用C++标准库中的set容器来处理和存储字符串的方法。通过将输入字符串标准化并拆分为单词,然后插入到set中,可以确保每个单词仅出现一次且按字母顺序排列。此方法适用于文本处理任务,如创建单词字典。
514

被折叠的 条评论
为什么被折叠?



