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——安迪的第一个字典(摘)的更多相关文章

  1. 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 ...

  2. [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary

    1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...

  3. 安迪的第一个字典(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 ...

  4. 安迪的第一个字典(Andy's First Dictionary,Uva 10815)

    输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出.单 词不区分大小写. 样例输入: Adventures in Disneyland Two blondes were goin ...

  5. UVa 10815 安迪的第一个字典

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. 安迪的第一个字典(UVa10815)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  7. 安迪的第一个字典 (Andy's First Dictionary,UVa10815)

    题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...

  8. 5_3 安迪的第一个字典(UVa10815)<set的使用>

    Andy 是个 8 岁的孩子,他梦想要制作一本他自己的字典. 这并不是一件容易的事,因为他认识的字还不是那么多. 他并不想要自己去想这本字典中该放哪些字,他有个聪明的主意. 就是拿出他最喜欢的一本故事 ...

  9. 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 ...

随机推荐

  1. log4j参数说明

    log4j.properties 使用 一.参数意义说明 输出级别的种类 ERROR.WARN.INFO.DEBUG ERROR 为严重错误 主要是程序的错误 WARN 为一般警告,比如session ...

  2. Ubuntu安装samba服务器

    一.安装软件 sudo apt-get install samba 二.配置samba服务器/etc/samba/smb.conf 把默认的配置文件备份,按如下修改配置文件 [global] work ...

  3. Mvc音乐商店demo的ajax异步删除功能总结

    刚刚从学校出来参加工作,没啥工作经验,所以各位大神们不要嘲笑哈! 来公司后要进行培训,给我们的作业中有一个使用 dapper+mvc+ajax+SQL Server 2008,来实现一个音乐商店的de ...

  4. 不到30行JS代码实现的Excel表格

    不到30行JS代码实现的Excel表格,jQuery并非不可替代 某国外程序员展示了一个由原生JS写成不依赖第三方库的,Excel表格应用,有以下特性: 由不足30行的原生JavaScript代码实现 ...

  5. CentOS 6.4 下安装 Apache

    下载地址:http://mirror.bit.edu.cn/ 参数 描述 prefix 安装目录 enable-rewrite 开启 rewrite 模块 sysconfdir 配置文件目录 ./co ...

  6. information_schema.key_column_usage 学习

    information_schema.key_column_usage 表可以查看索引列上的约束: 1.information_schema.key_column_usage 的常用列: 1.cons ...

  7. BZOJ 3529 数表(莫比乌斯反演)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3529 思路:令F(i)为i的约数和, 1<=x<=n,1<=y<=m G(i ...

  8. Keil C51 与 ARM 并存的方法

    很多朋友都在想,怎么让keil C51与ARM能够并存使用.有安装经验的朋友都知道,安好C51后再安ARm,C51不能正常工作:安好ARM后再安C51,ARM不能正常工作. 网上也有相关解决办法,不过 ...

  9. QTableView使用HTML显示富文本

    对于QTableView中的显示,我们前面介绍过很多种,其中包括:文本.进度条.复选框等,今天我们介绍一下关于富文本的显示. 可能绝大多数小伙伴会通过QAbstractTableModel中的data ...

  10. HttpApplication实战大文件上传 (第四篇)

    一.Asp.net中的文件上传 在Asp.net 1.1中,文件在上传过程中将被全部保存在内存中,对于大文件来说,会造成内存空间的过度使用,可能会招致恶意攻击.为了解决这个问题,Asp.net在配置文 ...