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 ...
随机推荐
- 【转载】安装和使用花生壳(linux)
安装和使用花生壳(linux) 一.安装说明(以CentOS 5为例) 1.安装必要的开发包 [root@localhost ~]# yum install gcc gcc-c++ autoconf ...
- Windows I/O模型之一:Select模型
1.概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock) 四种调用模式: 同步:所谓同步,就是在发出一个功能调用时,在没有得到结果 ...
- C++ 语言特性的性能分析
转载:http://www.cnblogs.com/rollenholt/archive/2012/05/07/2487244.html 大多数开发人员通常都有这个观点,即汇编语言和 C 语 ...
- 极客”一词,来自于美国俚语“geek”的音译,一般理解为性格古怪的人
起源 “ 极客”一词,来自于美国俚语“ geek”的音译,一般理解为性格古怪的人.数学“极客”大多是指,并不 一定是数学专业但又对数学等技术有狂热的兴趣并投入大量时间钻研的人.又 译作“ 奇客”.以前 ...
- android listview Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
android listview 适配器在多种类型viewType报错: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; ...
- spring中连接池的配置
在默认通过myeclipse生成的配置里,spring使用的是apache的dbcp连接池 <bean id="dataSource" class="org.apa ...
- laravel 心得
1.安装 使用composer安装laravel ,切换到你想要放置该网站的目录下,运行命令: composer create-project laravel/larevel project 4.1 ...
- img元素高度多出来的几像素
HTML: <div class="test"><img src="body2.jpg" alt=""></d ...
- PIL参考手册
Python Imaging Library Handbook http://effbot.org/imagingbook/pil-index.htm 随机验证码的产生 import Image, I ...
- 谈一谈JVM内存JAVA_OPTS参数
最近几个月,做的性能测试项目中,发现了一些内存方面的问题,其中有涉及到对JBOSS里的JAVA_OPTS配置,例如一下所示: JAVA_OPTS="-server -Xms1536m -Xm ...