set就是数学上的集合——每个元素最多只出现一次,和sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符。
问题描述
输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单词不区分大小写。
样例输入
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the road.The sign read: "Disneyland Left."
So they went home.
样例输出(为了节约篇幅只保留前五行)
a
adventures
blondes
came
disneyland
示例代码
#include
#include
#include
#include
using namespace std;
set dict;//string集合
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(s);//字符串流的输入输出while (ss >> buf) {//一个字符串一个字符串的来dict.insert(buf);//插入dict中并从小到大排序}}for (set::iterator it = dict.begin(); it != dict.end(); ++it) {//遍历输出cout << *it << "\n";}return 0;
}
分析
iterator的意思是迭代器,是STL中的重要概念,类似于指针。和“vector类似于数组一样”,这里的“类似”指的是用法类似。
map就是i从键(key)到值(value)的映射。因为重载了[]运算符,map像是数组的“高级版”。然后可以用一个map
问题描述
输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排列(所有大写字母在所有小写字母的前面)。
样例输入
ladder came tape soon leader acme RIDE lone Dreis peat
ScALE orb eye Rides dealer NotE derail LaCes drIed
noel dire Disk mace Rob dries
#
样例输出
Disk
NotE
derail
drIed
eye
ladder
soon
示例代码
#include
#include
#include
#include
#include