『写在前面的一些基础语法』
1.定义 和 赋值
- set<int> a={1,2};
- set<char> b={"1","2"};
- set<struct> c;
- set<vector<int>> d={a};
2.Set的一些特性
- Set就是数学上的集合,所以每个元素是不出重复的
- 自动排序(根据元素类型不同排序规则不同)
3.一些基础的操作
- a.insert(x) //插入一个元素x,若重则忽略
- 迭代器(iterator):
a.end();//返回的迭代器指向最后一个元素的后一个位置;
a.begin();//¨返回的迭代器指向 set 中的最小值;
a.rend;//返回的迭代器指向第一个元素的前一个位置;
a.rbegin();//返回的迭代器指向 set 中的最大值;
- a.size();//成员的个数
- a.empty();//检查集合是否为空
- a.clear();//清空集合
- a.erase(x);//删除元素x
- a.find(x);//¨返回 x 元素的迭代器,如果找不到 x 就返回 end() 的 迭代器。
- a.count(x);//统计元素x在集合出现的次数(不常用)
- a.lower_bound(x) //返回 set 中大于等于 x 的最小元素的迭代器
- a.upper_bound(x) //返回 set 中大于 x 的最小元素的迭代器。 如果找不到也会返回 end() 的迭代器
4.来个双胞胎
Multiset:最大的区别在于multiset中的元素可以重复
『上题上题』
安迪的第一个字典(UVa 10815)
【问题描述】
输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单词不区分大小写。
【样例输入】
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
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
『代码代码』
#include <iostream>
#include <sstream>
#include <bits/stdc++.h>
#include <set>
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(s);//从字符串s中提取每个单词到ss中
while(ss >> buf) //将ss中的每个单词赋值给buf
dict.insert(buf); //将每个buf插入到集合dict中
}
for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
cout<<*it<<"\n";
return 0;
}
『最后的一点点感悟』
- STL的确是神通广大,集合多半用于去重和排序(排序时 元素要有规则可用重载了'<'运算符之类)
- 处理字符串时要将一些"不合规则的元素"变得统一起来
1.大小写的统一转换
2.多余元素的统一的转化,例如本题中就将非英文字符转换为了空格
- 牛羊的电脑坏了,五百软妹币又么得了,233333