STL关联式容器之set和multiset
一,set和multiset的基础知识
1.set和multiset的基础
- set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按照一定的顺序排列,元素插入过程是按照排序规则插入的。所以不能指定插入元素的位置。
- set的底层数据结构是红黑二叉树,红黑树属于平衡二叉树。在插入操作和删除操作比vector快。
- set不可以直接存取元素,即不能使用像序列式容器中(vector,deque)那样随机存取。
- multiset和set的区别在于set支持元素唯一,而multiset允许插入多个重复的元素。
- 不可以直接修改set和multiset容器中的元素值,因为该类容器是自动排序的,如果想要修改某个元素的值,需要先删除该元素,然后再重新插入新的元素。
- 要使用set或者multiset要引入的头文件是# include<set>。
2.函数对象的基础知识
set集合默认是从小到大的顺序排序的,如果我们存储的是学生类,想要自定义排序,那么STL提供了函数对象这个概念,所谓的函数对象就是重载了结构体的()操作符。然后返回结果是bool类型。
二,set和multiset的代码示例
1.set和multiset的基本比较
# include<iostream>
# include<set>
# include<string> using namespace std; int main01()
{
// 定义set集合
set<string> s1;
// 定义multiset集合
multiset<string> s2; // 往set集合中插入元素,其返回结果是一个pair,包含插入进去后的迭代器以及是否插入成功的标志
pair<set<string>::iterator, bool> p1 = s1.insert("Hello");
cout << *p1.first << "," << p1.second << endl;
// 我们发现往set集合中插入相同的元素会插入不成功
pair<set<string>::iterator, bool> p2 = s1.insert("Hello");
cout << *p2.first << "," << p2.second << endl;
// 遍历
for (string tmp : s1)
{
cout << tmp << endl;
}
// 我们往multiset集合中插入元素
s2.insert("Hello");
s2.insert("Hello");
for (string tmp : s2)
{
cout << tmp << endl;
} return ;
}
2.set集合的遍历
# include<iostream>
# include<string>
# include<set>
using namespace std; int main02()
{
// 定义集合
set<string> s;
// 插入集合
s.insert("Hello");
s.insert("world");
s.insert("C++");
// 增强for遍历
for (string tmp : s)
{
cout << tmp << " ";
}
cout << endl;
// 迭代器正向遍历
for (set<string>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// 迭代器逆向遍历
for (set<string>::reverse_iterator it = s.rbegin(); it != s.rend(); it++)
{
cout << *it << " ";
}
cout << endl; return ;
}
3.set集合的删除
# include<iostream>
# include<set>
# include<string> using namespace std; int main03()
{
// 创建集合
set<string> s;
// 插入元素
s.insert("Hello");
s.insert("World");
s.insert("Unreal");
s.insert("C++");
// 删除元素World
for (set<string>::iterator it = s.begin(); it != s.end();)
{
if (*it == "World")
{
it = s.erase(it);
}
else {
it++;
}
}
// 输出集合的长度
int size1 = s.size();
cout << "size1 = " << size1 << endl;
// 遍历元素删除
set<string>::iterator it = s.begin();
while (!s.empty())
{
cout << *it << endl;
it = s.erase(it);
}
// 输出集合的长度
int size2 = s.size();
cout << "size = " << size2 << endl; return ;
}
4.set常用的函数
# include<iostream>
# include<set>
# include<string> using namespace std; int main04()
{
// set集合默认是从小到大排序的
set<int> s;
// 插入元素
s.insert();
s.insert();
s.insert();
s.insert();
// 遍历元素
for (int tmp : s)
{
cout << tmp << " ";
}
cout << endl;
// 统计元素出现的次数
int cs = s.count();
cout << "cs = " << cs << endl;
// 查找元素为2的迭代器
set<int>::iterator it = s.find();
cout << *it << endl; return ;
}
5.自定义函数对象实现对学生类的排序
# include<iostream>
# include<string>
# include<set>
using namespace std;
/*
定义学生类,按照其年龄进行排序
*/
class Student
{
public:
string name;
int age;
public:
Student(string name, int age)
{
this->name = name;
this->age = age;
}
};
/*
定义函数对象:所谓的函数对象,就是重载了()操作符的结构体
*/
struct StudentFunctor
{
bool operator()(const Student& stu1,const Student& stu2)
{
// 如果是>则是从大到小排序,如果是<则是从小到大排序
return stu1.age > stu2.age;
}
}; int main()
{
// 定义学生集合
set<Student,StudentFunctor> s;
// 插入学生
s.insert(Student("张飞",));
s.insert(Student("关羽", ));
s.insert(Student("赵云", ));
s.insert(Student("马超", ));
// 打印学生
for (Student stu : s)
{
cout << stu.name << " = " << stu.age << endl;
} return ;
}
STL关联式容器之set和multiset的更多相关文章
- STL关联式容器之set\map ----以STL源码为例
关联式容器的特征:所用元素都会根据元素的键值自动被排序. set STL 中的关联式容器低层数据结构为红黑树,其功能都是调用低层数据结构中提供的相应接口. set元的元素不会像map那样同时拥有键(k ...
- STL关联式容器之map和multimap
一,map和multimap的概念 1.map和multimap的基本知识 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中 ...
- STL序列式容器之list
一,list容器基本概念 1.list容器基本知识 list容器的底部数据结构为双向链表,可以高效的进行插入和删除元素. list因为底层数据结构是双向链表,因此不支持下标操作和.at()函数的操作. ...
- STL——关联式容器
一.关联式容器 标准的STL关联式容器分为set(集合)/map(映射表)两大类,以及这两大类的衍生体multiset(多键集合)和 multimap(多键映射表).这些容器的底层机制均以RB-tre ...
- C++ 序列式容器之vector
什么是容器 容器,顾名思义,是用来容放东西的场所.C++容器容放某种数据结构,以利于对数据的搜寻或排序或其他特殊目的.众所周知,常用的数据结构不外乎:数组array, 链表list, 树tree ...
- STL的六大容器之iterator----自定义范式
STL的iterator组件,分离了容器和算法. 一.规定 在STL体系下定义iterator,要满足规定的一些规范: 1.iterator_category 有5中分类,决定胃具体的操作,如:++, ...
- 关联式容器(associative containers)
关联式容器(associative containers) 根据数据在容器中的排列特性,容器可分为序列式(sequence)和关联式(associative)两种. 标准的STL关联式容器分为set( ...
- iBinary C++STL模板库关联容器之map/multimap
目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...
- [知识点]C++中STL容器之map
UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...
随机推荐
- maven 解决Cannot change version of project facet Dynamic web module to 2.5
我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...
- Shiro的Filter机制详解---源码分析(转)
Shiro的Filter机制详解 首先从spring-shiro.xml的filter配置说起,先回答两个问题: 1, 为什么相同url规则,后面定义的会覆盖前面定义的(执行的时候只执行最后一个). ...
- BestCoder Round #11 (Div. 2) 前三题题解
题目链接: huangjing hdu5054 Alice and Bob 思路: 就是(x,y)在两个參考系中的表示演全然一样.那么仅仅可能在这个矩形的中点.. 题目: Alice and Bob ...
- Setup iOS Development Environment.
Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...
- C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...
- Best Practices for QML and Qt Quick
Despite all of the benefits that QML and Qt Quick offer, they can be challenging in certain situatio ...
- 在windows下远程访问linux服务器
在网络性能.安全性.可管理性上,Linux有着其他系统无法比拟的强大优势,而服务器对这些方面要求特别高,因此Linux常常被用来做服务器使用.而当我们需要维护linux服务器的时候,就需要远程访问li ...
- Drupal 7 模块开发 建立模块帮助信息(hook_help)
建立模块请參考 <Drupal 7 模块开发 建立> 假设你要支持中文,文件格式必须保存为 UTF-8.NO BOM ------------------------------ hook ...
- apt-get install安装软件时出现依赖错误解决方式
在使用apt-get install安装软件时,常常会遇到如上图所看到的错误.该错误的意思为缺少依赖软件.解决方式为: aptitude install golang-go
- js如何实现页面跳转(大全)
js如何实现页面跳转(大全) 一.总结 一句话总结: 1.location的href属性: js跳转主要是通过window的location对象的href属性,因为location对象本来就是表示的浏 ...