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. 总结前端JQ常用的一些操作手法(慢慢完善)

    1.实例化Js一个object对象,把它当做类来用,事例是操作url的参数 function GetRequestCondition() { var url = window.location.hre ...

  2. fddd

    <script language="JavaScript" type="text/javascript"> function exportChart ...

  3. php word转HTML

    因为安装的的xampp不知道如何查看我的Apache版本是多少,就先把com.allow_dcom=true打开了,但是仍旧报错说找不到com类,然后就把下面的extension扩展添加到php.in ...

  4. information_schema.columns 学习

    每一个表中的每一列都会在information_schema.columns表中对应一行 1.informaiton_schema.columns 常用列: 1.table_catalog :不管是t ...

  5. sql helper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  6. IOS开发UI篇-NavigationController的基本使用

    一.简介 一个iOS的app很少只由一个控制器组成,除非这个app极其简单 当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小v ...

  7. You and your research

    英文版http://www.cs.virginia.edu/~robins/YouAndYourResearch.html 视频版http://www.youtube.com/watch?v=a1zD ...

  8. 格而知之4:寻找EXC_BAD_ACCESS

    EXC_BAD_ACCESS算是一个比较常见的错误,大部分情况下,它出现在某个对象还未初始化或已被释放后,还去试图访问这个对象的时候,即是在出现悬挂指针的时候(当然也有非悬挂指针导致的EXC_BAD_ ...

  9. Splay POJ3468(老题新做)

    A Simple Problem with Integers Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d ...

  10. Java 线程第三版 第八章 Thread与Collection Class 读书笔记

        JDK1.2引入最有争议性的改变是将集合类默觉得不是Thread安全性的. 一.Collection Class的概述 1. 具有Threadsafe 的Collection Class: j ...