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. PHP学习笔记三十三【自定义错误处理器】

    <?php //自定义错误处理器 //$errorno 错误号 //$errmes错误信息 //这两个参数是必须的 function my_error($errorno,$errmes) { e ...

  2. POJ 1269 - Intersecting Lines 直线与直线相交

    题意:    判断直线间位置关系: 相交,平行,重合 include <iostream> #include <cstdio> using namespace std; str ...

  3. POJ 2455 Secret Milking Machine (二分 + 最大流)

    题目大意: 给出一张无向图,找出T条从1..N的路径,互不重复,求走过的所有边中的最大值最小是多少. 算法讨论: 首先最大值最小就提醒我们用二分,每次二分一个最大值,然后重新构图,把那些边权符合要求的 ...

  4. php+mysql+pdo连接数据库

    1.$pdo = new PDO("mysql:host=localhost;dbname=test","root","123456");/ ...

  5. 使用URLConnection获取网页信息的基本流程

    参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...

  6. JQuery validator扩展

    //validator 扩展 jQuery.validator.addMethod("mail", function(value, element, messages) { ret ...

  7. SSE2 Intrinsics各函数介绍[转]

    SIMD相关头文件包括: //#include <ivec.h>//MMX //#include <fvec.h>//SSE(also include ivec.h) //#i ...

  8. ReferenceError: $ is not defined

    蛋疼的问题,原因是jquery导入顺序不对,任何页面都必须把jquery的导入放在首位,js文件放在其次.

  9. 根据查询结果创建新表create table .. as (select....)

    一张表 student 查询数学和英语都是90分以上的同学,并创建一张新表 test1

  10. PIL参考手册

    Python Imaging Library Handbook http://effbot.org/imagingbook/pil-index.htm 随机验证码的产生 import Image, I ...