STL之set(唯一且有顺序)
set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,
在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。
方法 用法
插入删除
insert(value) 向集合中插入一个元素
erase() 擦除元素中的一个或者一段元素
clear() 清除集合中的元素
查找
find() 查找value的值,返回下标位置,否则,返回最后一个元素后面一个位置(即迭代器的end)
容量
empty() 判断集合是否为空
size() 返回集合中的元素个数
max_size() 返回集合的最大容量
迭代器
begin() 返回开头的迭代器
end() 返回结尾的迭代器
rbegin() 反向遍历的首个位置迭代器
rend() 反向遍历的最后位置的迭代器
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> s;
s.insert();
s.insert();//将key_value插入到set中
s.insert();
s.insert();
s.insert();
s.insert();
cout<<"set的size值为:"<<s.size()<<endl;//输s.size()出该set集合中的元素个数
cout<<"set的maxsize的值为:"<<s.max_size()<<endl;//max_size()输出地址
cout<<"set中的第一个元素是:"<<*s.begin()<<endl;//begin()输出最小的元素
cout<<"set中的最后一个元素是:"<<*s.end()<<endl;//end()输出最大的元素
cout<<"set中的是否出现过该元素是:"<<s.count()<<endl;//s.count(1)可以判断是否出现过该元素,出现为1,未出现为0
//equal_range()分别表示第一个大于或等于给定元素和第一个大于元素
pair<set<int>::const_iterator,set<int>::const_iterator> pr;
pr = s.equal_range();
cout<<"第一个大于等于 2 的数是 :"<<*pr.first<<endl;
cout<<"第一个大于 2的数是 : "<<*pr.second<<endl;
s.clear();//清空集合
if(s.empty())//判断集合是否为空
{
cout<<"set为空!!!"<<endl;
}
cout<<"set的size值为:"<<s.size()<<endl;
cout<<"set的maxsize的值为:"<<s.max_size()<<endl; int a[] = {,,};//建立set集合二
set<int> s2(a,a+);
set<int>::iterator iter;
//find(),返回给定值值得定位器,如果没找到则返回end()。
if((iter = s2.find()) != s2.end()) //如果该数存在则会输出括号里的信息
{
cout<<"测试"<<*iter<<endl;
}
int b[]={,,};
s2.insert(b,b+);//将定位器first到second之间的元素插入到set中
cout<<"set的size值为:"<<s2.size()<<endl;//返回s2中的元素个数
cout<<*s2.lower_bound()<<endl;//lower_bound(8)返回第一个大于等于key_value的定位器
cout<<*s2.upper_bound()<<endl; //upper_bound(8)返回最后一个大于等于key_value的定位器
}
uva 10815代码:
#include<iostream>
#include<set>
#include<string.h>
#include<sstream>
using namespace std;
int main()
{
set<string>s;
string h,buf;
while(cin>>h)
{
for(int i=;i<h.length();i++)
{
if(isalpha(h[i])) //判断一个字符是否为字母
h[i]=tolower(h[i]);
else
h[i]=' ';
}
stringstream ss(h);//定义了一个字符串流,并用一个字符串初始化
while(ss>>buf)//将buf读出到ss
s.insert(buf); }
//输出时需要运用迭代器
set<string>::iterator it;
for(it=s.begin();it!=s.end();it++)
cout<<(*it)<<"\n";
return ;
}
代码解析:
#include<iostream>
#include<set>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string s,buf;
set<string> str;
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来初始化
while(ss>>buf)//由于ss中可能有空格,这个就是以空格为界限 循环输入到buf(buf是不含空格的字符串(即单词))中
str.insert(buf);
}
for(set<string>::iterator it=str.begin();it!=str.end();it++)
cout<<*it<<endl;
getchar();
}
定义比较函数 例题:
typedef struct mycmp //自定义比较函数
{
bool operator()(const int &a,const int &b)
{
return a>b;
}
};
#include<iostream>
#include<set>
#include<string.h>
#include<sstream>
using namespace std;
typedef struct mycmp //自定义比较函数
{
bool operator()(const int &a,const int &b)
{
return a>b;//改变这里的>号改变顺序大小
}
};
int main()
{
set<int,mycmp>s;
s.insert();
s.insert();
s.insert();
for(int i=;i<;i++)//顺序输入值
s.insert(i); cout<<"顺序输出:"<<endl;
set<int>::iterator it;
for(it=s.begin();it!=s.end();it++)
cout<<*it<<" "; cout<<endl<<"反序输出:"<<endl;
set<int>::reverse_iterator reit;
for(reit=s.rbegin();reit!=s.rend();reit++)
cout<<*reit<<" ";
return ;
}
定义数据类型 结构体例题:
typedef struct mycmp
{
string name;
float score;
bool operator<(const mycmp &a)const//自定义比较函数
{
return a.score<score;//可更改这处的>而更改输出顺序
}
};
#include<iostream>
#include<set>
#include<string.h>
#include<sstream>
using namespace std;
typedef struct mycmp
{
string name;
float score;
bool operator<(const mycmp &a)const//自定义比较函数
{
return a.score<score;//可更改这处的>而更改输出顺序
}
};
int main()
{
set<mycmp> v;
mycmp s;
s.name="Jack";
s.score=80.5;
v.insert(s);
s.name="Nacy";
s.score=60.5;
v.insert(s);
s.name="Tomi";
s.score=;
v.insert(s); set<mycmp>::iterator p;
for(p=v.begin(); p!=v.end(); p++)
{
cout<<(*p).name<<" : "<<(*p).score<<endl;
}
return ;
}
输出结果:(按crtl+z+alt)
Jack : 80.5
Nacy : 60.5
Tomi : 20
例题一:01串排序:将01串首先按长度排序,长度相同按1的个数排序,1的个数相同按ASCII码排序
输入:
10011111
00001101
1010101
1
0
1100
解题想法:使用set自定义比较函数,先看字符串长度,长度从小到大,如果长度相同,比较1的个数,我们这里用#include<algorithm>中的count(s1.begin(),s1.end(),'1')来计算1的个数,如果1的个数相同,按ascii码排序,个数不同,按个数大小排序
struct Comp
{
bool operator () (const string &s1,const string &s2)
{
if(s1.length()!=s2.length())
return(s1.length()<s2.length());
int c1=count(s1.begin(),s1.end(),'1');
int c2=count(s2.begin(),s2.end(),'1');
if(c1!=c2)
return (c1<c2);
else
return(s1<s2);
}
};
#include<iostream>
#include<fstream>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
struct Comp
{
bool operator () (const string &s1,const string &s2)
{
if(s1.length()!=s2.length())
return(s1.length()<s2.length());
int c1=count(s1.begin(),s1.end(),'');
int c2=count(s2.begin(),s2.end(),'');
if(c1!=c2)
return (c1<c2);
else
return(s1<s2);
}
};
int main()
{
set<string,Comp> s;
string t;
int n;
while(cin>>t)
{
s.insert(t);
}
set<string,Comp>::iterator it;
for(it=s.begin(); it!=s.end(); it++)
cout<<*it<<endl;
return ;
}
例题二:输入若干个时间,对其进行排序,从小到大输出
解题思路:定义自定义struct数据类型
struct time
{
int hour;
int minute;
int second;
bool operator < (const time &a)const
{
if(hour!=a.hour)
return(hour<a.hour);
else if(minute!=a.minute)
return(minute<a.minute);
else
return(second<a.second);
}
};
输入:
3
12:59:30
1:20:40
1:20:30
输出:
1:20:30
1:20:40
12:59:30
#include<iostream>
#include<fstream>
#include<set>
using namespace std
struct time
{
int hour;
int minute;
int second;
bool operator < (const time &a)const
{
if(hour!=a.hour)
return(hour<a.hour);
else if(minute!=a.minute)
return(minute<a.minute);
else
return(second<a.second);
}
};
int main()
{
set<struct time> s;
struct time t;
int n;
cin>>n;
for(int i=; i<n; i++)
{
cin>>t.hour;
cin.get();
cin>>t.minute;
cin.get();
cin>>t.second;
s.insert(t);
}
set<struct time>::iterator it;
for(it=s.begin(); it!=s.end(); it++)
cout<<(*it).hour<<":"<<(*it).minute<<":"<<(*it).second<<endl;
return ; }
STL之set(唯一且有顺序)的更多相关文章
- C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...
- STL六大组件之——容器知识大扫盲
STL中的容器主要涉及顺序容器类型:vector.list.deque,顺序容器适配器类型:stack.queue.priority_queue.标准库中的容器分为顺序容器和关联容器.顺序容器(seq ...
- STL用法大全
1. 概述 泛型编程思想最早缘于A.Stepanov提出的部分算法可独立于数据结构的论断.20世纪90年代初A.Stepanov和Meng Lee根据泛型编程的理论用C++共同编写了STL.但直 ...
- STL使用总结
转载于http://blog.csdn.net/daisy_chenting/article/details/6898184 1. 概述 泛型编程思想最早缘于A.Stepanov提出的部分算法可 ...
- 《STL源码剖析》——第一、二、三章
第一章:概论: 换句话说,STL所实现的,是依据泛型思维架设起来的一个概念结构.这个以抽象概念(abstract concepts)为主体而非以实际类(classes)为主体的结构,形成了一个严谨的 ...
- 关于Windows API、CRT和STL二三事
1.本文编写目的 本文是为了帮助一些人弄清一些关于Windows API, C运行时程序库(CRT), 和标准C++库(STL)的基本概念.有很多人甚至是有经验的程序员在这些概念上是含糊不清的甚 ...
- [C++]STL中的容器
C++11 STL中的容器 一.顺序容器: vector:可变大小数组: deque:双端队列: list:双向链表: forward_list:单向链表: array:固定大小数组: string: ...
- sql server中查询结果集顺序问题
因为优化器可能会选择并行处理,或者在多文件情况下不按“期待”顺序扫描数据,所以无法保证数据的顺序.唯一能确保顺序的只有order by. 并行处理的过程导致顺序不一致,单核上不存在并行,而双核,可能使 ...
- C++利用动态数组实现顺序表(不限数据类型)
通过类模板实现顺序表时,若进行比较和遍历操作,模板元素可以通过STL中的equal_to仿函数实现,或者通过回调函数实现.若进行复制操作,可以采用STL的算法函数,也可以通过操作地址实现.关于回调函数 ...
随机推荐
- 生产者与消费者-1:N-基于list
一个生产者/多个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...
- IP地址及子网掩码计算
主机号全0表示网络号,主机号全1表示广播地址 我们都知道,IP是由四段数字组成,在此,我们先来了解一下3类常用的IP A类IP段 0.0.0.0 到127.255.255.255 B类IP段 128. ...
- 基于XML的DI
三.集合属性注入(包含:为数组注入值.为List注入值.为Set注入值.为Map注入值.为Properties注入值) 集合类定义如下: xml定义如下:仔细看 下面是执行代码: 四.对于 ...
- [raspberry pi3] 编译安装chromium
想要试试arm板上使用selenium是不是可能,发现Firefox什么的不顶用,网上有提供的chrome的安装手顺,但是没有arm版本的对应的chromedriver,只能自己搞了. 这边介绍的方法 ...
- Hibernate常见异常总结
系统配置 1.没有查找到src目录下的hibernate.cfg.xml Exception in thread "main" org.hibernate.HibernateExc ...
- git 命令总结(转)
结构图: <1> Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新建代码库 # 在当前目录新建 ...
- 转载 【Linux】Linux中常用操作命令
[Linux]Linux中常用操作命令 https://www.cnblogs.com/laov/p/3541414.html#vim Linux简介及Ubuntu安装 常见指令 系统管理 ...
- 调停者模式Mediator(中介者模式)详解
原文链接:https://www.cnblogs.com/java-my-life/archive/2012/06/20/2554024.html 在阎宏博士的<JAVA与模式>一书中开头 ...
- CF351A Jeff and Rounding 思维
Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, ...
- CF431D Random Task 二分+数位dp
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. ...