stl的集合set——安迪的第一个字典(摘)
set就是数学上的集合——每个元素最多只出现一次,和sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符
以下代码测试set中无重复元素
#include<iostream>
#include<iterator>
#include<set>
using namespace std;
typedef long long LL;
const int coeff[]={,,};
int main()
{
set<LL>s;
s.insert();
s.insert();
s.insert();
set<LL>::iterator iter;
for(iter=s.begin();iter!=s.end();iter++){
cout<<*iter<<endl;
}
//system("pause");
return ;
}
(ps:multiset是允许有重复数据的集合)
set不支持随机访问,必须要使用迭代器去访问。
begin() 返回指向第一个元素的迭代器
clear() 清除所有元素
count() 返回某个值元素的个数
empty() 如果集合为空,返回true(真)
end() 返回指向最后一个元素之后的迭代器,不是最后一个元素
erase() 删除集合中的元素
find() 返回一个指向被查找到元素的迭代器
insert() 在集合中插入元素
max_size() 返回集合能容纳的元素的最大限值
size() 集合中元素的数目
swap() 交换两个集合变量
例题
输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出,单词不区分大小写
样例

代码
#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=;i<s.length();i++)
if(isalpha(s[i]))s[i]=tolower(s[i]);
else s[i]=' ';
stringstream ss(s); //创建存储s的副本的 stringstream 对象ss
while(ss>>buf)dict.insert(buf); //将转换后的小写单词存入集合中
}
for(set<string>::iterator it=dict.begin();it!=dict.end();++it)cout<<*it<<endl;
//system("pause");
return ;
}
isalpha()判断字符ch是否为英文字母,若为小写字母,返回2,若为大写字母,返回1。若不是字母,返回0。
tolower()把字符转换成小写字母,非字母字符不做出处理
stl的集合set——安迪的第一个字典(摘)的更多相关文章
- STL语法——集合:set 安迪的第一个字典(Andy's First Dictionary,UVa 10815)
Description Andy, , has a dream - he wants to produce his very own dictionary. This is not an easy t ...
- [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary
1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...
- 安迪的第一个字典(Andy's First Dictionary,UVa 10815)
Description Andy, , has a dream - he wants to produce his very own dictionary. This is not an easy t ...
- 安迪的第一个字典(Andy's First Dictionary,Uva 10815)
输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出.单 词不区分大小写. 样例输入: Adventures in Disneyland Two blondes were goin ...
- UVa 10815 安迪的第一个字典
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 安迪的第一个字典(UVa10815)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- 安迪的第一个字典 (Andy's First Dictionary,UVa10815)
题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...
- 5_3 安迪的第一个字典(UVa10815)<set的使用>
Andy 是个 8 岁的孩子,他梦想要制作一本他自己的字典. 这并不是一件容易的事,因为他认识的字还不是那么多. 他并不想要自己去想这本字典中该放哪些字,他有个聪明的主意. 就是拿出他最喜欢的一本故事 ...
- set的运用 例题5-3 安迪的第一个字典(Andy's First Dictionary,Uva 10815)
#include<bits/stdc++.h>using namespace std;set<string> dict;int main(){ string s, buf; w ...
随机推荐
- PagedList 分页
@using PagedList.Mvc;@model PagedList.IPagedList<MvcApplicationBootStramp.Models.Person> @{ ...
- iOS学习资料整理
视频教程(英文) 视频 简介 Developing iOS 7 Apps for iPhone and iPad 斯坦福开放教程之一, 课程主要讲解了一些 iOS 开发工具和 API 以及 iOS S ...
- javaScript动态添加样式
[动态添加css样式] <html> <head> <script type="text/javascript"> window.onload= ...
- nginx启动报错(1113: No mapping for the Unicode character exists in the target multi-byte code page)
使用windows版本的nginx启动时遇到(1113: No mapping for the Unicode character exists in the target multi-byte co ...
- gcc 编译的4个过程简单识记
直入正题,测试编译代码如下: lude <stdio.h> int main() { ,y,z; x*=(y=z=); printf("%d\n",x); z=; x= ...
- Tomcat学习笔记 - 错误日志 - NetBeans配置tomcat出错情况总结 -- 尚未授予访问 Tomcat 服务器的权限。请在服务器管理器的 Tomcat 定制器中设置 "manager-script" 角色的正确用户名和口令。 有关详细信息, 请查看服务器日志。
错误描述: 发布时控制台出现: 部署错误: 尚未授予访问 Tomcat 服务器的权限.请在服务器管理器的 Tomcat 定制器中设置 "manager-script" 角色的正确用 ...
- Oracle:ORA-01791: 不是 SELECTed 表达式
项目中写hql语句 出现 ORA-01791: 不是 SELECTed 表达式问题. 语句如下: select distinct(name) where student order by numbe ...
- try-catch-finally块的运行机制
try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 也许你的答案是在return之前,但往更细地说,我 ...
- spring springMVC mybatis 集成
最近闲来无事,整理了一下spring springMVC mybatis 集成,关于这个话题在园子里已经有很多人写过了,我主要是想提供一个完整的demo,涵盖crud,事物控制等. 整个demo分三个 ...
- JavaScript 之 Cookie
JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...