C++进阶-STL set/multiset容器和map容器的简单认识

set/multiset容器的简单认识

    • set基本概念
      • set与multiset 的区别:
      • set容器的构造和赋值
      • set容器的大小和交换
      • set容器的插入与删除
      • set容器的查找和统计
      • set容器-set和multiset的区别
      • set容器内置类型指定排序规则
      • set容器自定义数据类型指定排序规则
    • pair对组创建
    • map容器的基本概念
      • map容器构造和赋值
      • map容器大小和交换
      • map容器插入和删除
      • map容器查找和统计
      • map容器排序

set基本概念

set的所有元素在插入时会自动被排序,其本质 set/multiset 属于并联式容器,底层结构是用二叉树实现的

set与multiset 的区别:

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素

使用时仅需要包含一个 set 的头文件

#include <set>

set容器的构造和赋值

  • set<T> st; 默认构造函数
#include <iostream>
#include <string>
#include <set>
using namespace std;int main()
{//默认构造函数set<int> st;st.insert(12);
}
  • set(const set<T>& st); 拷贝构造函数
#include <iostream>
#include <string>
#include <set>
using namespace std;int main()
{//默认构造函数set<int> st;st.insert(12);//拷贝构造函数set<int> st2(st);
}
  • set& operator=(const set<T>& st); 重载等号操作符
#include <iostream>
#include <string>
#include <set>
using namespace std;int main()
{//默认构造函数set<int> st;st.insert(12);set<int> st2;//重载等号操作符赋值st2 = st;
}

set容器的大小和交换

用于统计set容器的大小以及交换set容器

  • size(); 返回容器中元素的数目
在这里插入代码片
  • empty(); 判断容器是否为空
#include <iostream>
#include <string>
#include <set>
using namespace std;int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}string str = (((int)st.empty()) == 0) ? "不为空" : "为空";std::cout << "容器st是否为空: " << str << std::endl;
}

在这里插入图片描述

  • swap(st); 交换两个集合容器
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const set<int> st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}set<int> st2;for (int i = 11; i < 20; i++){st2.insert(i);}std::cout << "执行交换前 st的数据信息为:" << std::endl;printf(st);std::cout << "执行交换前 st2的数据信息为:" << std::endl;printf(st2);st.swap(st2);std::cout << "执行交换后 st的数据信息为:" << std::endl;printf(st);std::cout << "执行交换后 st2的数据信息为:" << std::endl;printf(st2);
}

执行结果

set容器的插入与删除

  • insert(elem); 在容器中插入元素
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const set<int> st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);for (int i =100; i > 90; i--){st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);
}

运行结果
以上案例说明set容器在插入元素后,会进行排序

  • clear(); 清除所有元素
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const set<int> st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);st.clear();std::cout << "清空数据据后 st的数据信息为:" << std::endl;printf(st);
}

执行结果

  • erase(pos); 删除pos迭代器所指的元素,返回下一个元素速度迭代器
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const set<int> st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);set<int>::iterator it = st.erase(st.begin());std::cout << "删除st.begin元素后 返回的下一元素位置指向:" << *it << std::endl;std::cout << "删除st.begin元素后 set容器值列表为:" << std::endl;printf(st);
}

执行结果

  • erase(beg, end); 删除区间[beg, end)的所有元素,返回下一个元素的迭代器
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const set<int> st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);set<int>::iterator it = st.erase(st.begin(), ++(++st.begin()));std::cout << "删除[st.begin, ++(++st.begin()))元素后 返回的下一元素位置指向:" << *it << std::endl;std::cout << "删除元素后 set容器值列表为:" << std::endl;printf(st);
}

执行结果

  • earse(elem); 删除容器中值为elem的元素
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const set<int> st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);st.erase(1);std::cout << "删除元素 1 后 set容器值列表为:" << std::endl;printf(st);
}

执行结果

set容器的查找和统计

  • find(key); 查找key是否存在,若存在,返回该key值的元素的迭代器,若不存在,返回 set.end();
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const set<int> st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{set<int> st;for (int i = 0; i < 10; i++){st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);set<int>::iterator it = st.find(1);std::cout << "查找元素 1 的位置:" << &it << " 其指向的元素值为:"  << *it << std::endl;set<int>::iterator it_not_find = st.find(100);if (it_not_find == st.end()){std::cout << "元素100 未查找到" << std::endl;}
}

执行结果

  • count(key); 统计元素key的个数
#include <iostream>
#include <string>
#include <set>
using namespace std;void printf(const multiset<int> st)
{for (multiset<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}int main()
{multiset<int> st;for (int i = 0; i < 10; i++){st.insert(i);st.insert(i);st.insert(i);}std::cout << "插入数据后 st的数据信息为:" << std::endl;printf(st);int count = st.count(1);std::cout << "统计元素 1 的数量,元素共有:" << count << " 个:" << std::endl;
}

执行结果
这里使用multiset举例,因为set不会存储重复插入的元素,所有元素数量均为1个

set容器-set和multiset的区别

  • set不可以插入重复数据,而multiset可以插入重复数据
  • set插入数据的同时会返回插入结果,表示插入是否成功
  • multiset不会监测数据,因此可以插入重复数据
#include <iostream>
#include <string>
#include <set>
using namespace std;int main()
{set<int> st;pair<set<int>::iterator, bool> ret = st.insert(3);if (ret.second){std::cout << "插入成功" << std::endl;}else{std::cout << "插入失败" << std::endl;}ret = st.insert(3);if (ret.second){std::cout << "插入成功" << std::endl;}else{std::cout << "插入失败" << std::endl;}
}

执行结果
内部使用的 pair<set<int>::iterator, bool> 是一个键值对的结构,调用set.insert() 后,会触发重复值监测,如果检测到重复值,那么会导致返回插入失败的结果
multiset没有这个操作

set容器内置类型指定排序规则

set容器默认排序规则是从小到大,利用仿函数,可以改变排序规则

#include <iostream>
#include <string>
#include <set>
#include <ctime>
using namespace std;class CustomCompare
{
public://数据可能被修改,所以需要使用const限定调用函数的set对象不被修改bool operator()(int v1, int v2) const{return v1 > v2;}
};void printf(const set<int>& st)
{for (set<int>::const_iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}void printf_custom(set<int, CustomCompare>& st)
{for (set<int, CustomCompare>::iterator it = st.begin(); it != st.end(); it++){std::cout << *it << " ";}std::cout << std::endl;
}void init(set<int>& st)
{//随机数种子srand((unsigned int)time(NULL));for (int i = 0; i < 10; i++){st.insert(rand() % 60 + 40);}
}int main()
{set<int> st;init(st);std::cout << "初始化set后的值(当前是默认升序排列):" << std::endl;printf(st);//指定排序规则为从大到小 - 需要在创建容器的时候指定set<int, CustomCompare> st2;st2.insert(12);st2.insert(14);st2.insert(54);st2.insert(34);st2.insert(76);std::cout << "初始化set后的值(当前是定义了自定义仿函数排序规则):" << std::endl;printf_custom(st2);
}

运行结果

set容器自定义数据类型指定排序规则

#include <iostream>
#include <string>
#include <set>
#include <ctime>
using namespace std;class Person
{
private:string m_Name;int m_Age;
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}void printf() const{std::cout << "姓名:" << this->m_Name << " 年龄:" << this->m_Age << std::endl;}int getAge(){return this->m_Age;}
};class PersonCompare
{
public:bool operator()(Person p1, Person p2) const{return p1.getAge() > p2.getAge();}
};void printf(const set<Person, PersonCompare>& st)
{for (set<Person, PersonCompare>::const_iterator it = st.begin(); it != st.end(); it++){it->printf();}
}int main()
{set<Person, PersonCompare> st;Person p1("张三",12);Person p2("李四", 54);Person p3("王二", 23);Person p4("麻子", 87);Person p5("刘武", 45);Person p6("无硫", 22);st.insert(p1);st.insert(p2);st.insert(p3);st.insert(p4);st.insert(p5);st.insert(p6);printf(st);
}

执行结果

pair对组创建

成对出现的数据,利用对组可以返回两个数据

两种创建方式:

  • pair<type, type> p (value, value2);
  • pair<type, type> p = make_pair(value, value2);
#include <iostream>
#include <string>
using namespace std;int main()
{pair<string, int> p1("测试Pair", 111);std::cout << "插入的值为:(" << p1.first << ", " << p1.second << ")" << std::endl;p1 = make_pair("测试pair第二次", 121);std::cout << "插入的值为:(" << p1.first << ", " << p1.second << ")" << std::endl;
}

执行结果

map容器的基本概念

  • map中所有元素都是pair
  • pair中的第一个元素是key(键值),起到索引所用,第二个元素是value(实值)
  • 所有元素都会根据元素的键值自动排列

本质:

  • map/multimap 属于并联式容器,底层结构是用二叉树实现

优点:

  • 可以根据key值快速查找到value值

map和multimap 的区别:

  • map不允许容器中有重复的key值元素
  • multimap允许容器中有重复的key值元素

map容器构造和赋值

  • map<T1, T2> mp; 默认构造函数
#include <iostream>
#include <string>
#include <map>
using namespace std;int main()
{//默认构造函数map<string, int> mp;
}
  • map(const map<T1, T2>& mp); 拷贝构造函数
#include <iostream>
#include <string>
#include <map>
using namespace std;int main()
{//默认构造函数map<string, int> mp;//拷贝构造map<string, int> mp2(mp);
}
  • map& operator=(const map& map); 重载等号操作符
#include <iostream>
#include <string>
#include <map>
using namespace std;int main()
{//默认构造函数map<string, int> mp;//重载=号赋值map<string, int> mp2 = mp;
}

map容器大小和交换

  • size(); 返回容器中的元素的数目
#include <iostream>
#include <string>
#include <map>
using namespace std;int main()
{map<string, int> mp;std::cout << mp.size() << std::endl;
}
  • empty(); 判别容器中元素是否为空
#include <iostream>
#include <string>
#include <map>
using namespace std;int main()
{map<string, int> mp;mp.insert(pair<string,int>("测试", 12));std::cout << mp.empty() << std::endl;
}
  • swap(st); 交换两个集合容器
#include <iostream>
#include <string>
#include <map>
using namespace std;int main()
{map<string, int> mp;map<string, int> mp2;mp.swap(mp2);
}

map容器插入和删除

  • insert(elem); 在容器中插入元素
#include <iostream>
#include <string>
#include <map>
using namespace std;int main()
{map<string, int> mp;//第一种方法mp.insert(pair<string,int>("测试", 12));//第二种方法mp.insert(make_pair("测试2", 12));//第三种方法mp.insert(map<int, int>::value_type("测试3", 12))//第四种 不建议使用 没有这个key值调用时,会根据您的key值创建一个信息数据出来并填充默认值mp["测试5"] = 13for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++){std::cout << "(" << (*it).first << " ," << (*it).second << ")" << std::endl; }
}
  • clear(); 清除所有元素
map<string, int> mp;
mp.insert(pair<string,int>("测试", 12));
mp.clear();
  • erase(pos); 删除pos迭代器所指的元素,返回下一个元素的迭代器
map<string, int> mp;
mp.insert(pair<string,int>("测试", 12));
map<string, int>::iterator next = mp.erase(mp.begin());
  • erase(beg, end); 删除[beg, end)区间所有的元素,返回下一个元素的迭代器
map<string, int> mp;
mp.insert(pair<string,int>("测试", 12));
map<string, int>::iterator next = mp.erase(mp.begin(), mp.end());
  • erase(key); 删除容器中key值为key的元素
map<string, int> mp;
mp.insert(pair<string,int>("测试", 12));
mp.erase(12);

map容器查找和统计

  • find(key); 查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回map.end();
map<string, int> mp;
mp.insert(pair<string, int>("测试", 12));
map<string, int>::iterator find_result = mp.find("测试");
if (find_result == mp.end())
{std::cout << "未查找" << std::endl;
}
else
{std::cout << "查找到结果" << std::endl;
}
  • count(key); 统计key元素个数
multimap <string, int> mp;
mp.insert(pair<string, int>("测试", 12));
mp.insert(pair<string, int>("测试", 13));
int count =  mp.count("测试");
std::cout << "查找到结果" <<  count << "个" << std::endl;

map容器排序

利用仿函数,改变排序规则

#include <iostream>
#include <string>
#include <map>
using namespace std;class Compare
{
public:bool operator()(string k1, string k2) const{return k1 > k2;}
};int main()
{multimap <string, int , Compare> mp;mp.insert(pair<string, int>("测试1", 12));mp.insert(pair<string, int>("测试2", 13));for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++){std::cout << "(" << (*it).first << ", " << (*it).second << ")" << std::endl;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/169195.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2.OpenResty系列之Lua入门

1. Lua简介 Lua是一种轻量级的、高效的脚本编程语言&#xff0c;最初由巴西里约热内卢天主教大学的一个研究小组开发和发布。Lua的设计目标是提供一个简单、可嵌入、可扩展的脚本语言&#xff0c;官方实现完全采用 ANSI C 编写&#xff0c;能以 C 程序库的形式嵌入到其他应用程…

牛客、赛码网OJ调试(全)

现在无论开发还是测试&#xff0c;面试的时候都需要考察代码能力。 从测试的职业发展来看&#xff0c;现在市场上对于纯功能测试的需求很少&#xff0c;招聘方均要求面试者一方面具备测试基础能力&#xff0c;也要求有点代码能力。 对于测试来说&#xff0c;除了测试开发&#…

Matplotlib绘图一网打尽【持续更新ing】

2 绘制扇形图 绘制一个展示男女乘客比例的扇形图 得出男女的具体数字 sex_per df["Sex"].value_counts() sex_per # 把画图的包导入进来 import matplotlib.pyplot as plt# 这种绘图方式主要用于有多个子图以及复杂的图形布局的时候。fig,ax plt.subplots()# pl…

creo6.0教程之旋转,扫描

目录 一、旋转&#xff1a;二、扫描&#xff1a; 一、旋转&#xff1a; 案例1&#xff1a;旋转一个球&#xff1a; 任意一个平面绘制草图&#xff1a; 确定草图后&#xff0c;然后退出草图&#xff0c;点击旋转&#xff1a; 案例2&#xff1a;旋转一个杯子雏形&#xff1a; …

【入门Flink】- 09Flink水位线Watermark

在窗口的处理过程中&#xff0c;基于数据的时间戳&#xff0c;自定义一个“逻辑时钟”。这个时钟的时间不会自动流逝&#xff1b;它的时间进展&#xff0c;就是靠着新到数据的时间戳来推动的。 什么是水位线 用来衡量事件时间进展的标记&#xff0c;就被称作“水位线”&#x…

Nginx(五)

负载均衡 官网文档 Using nginx as HTTP load balancer nginx中实现反向代理的方式 HTTP&#xff1a;通过nginx配置反向代理到后端服务器&#xff0c;nginx将接收到的HTTP请求转发给后端服务器。使用 proxy_pass 命令 HTTPS&#xff1a;通过nginx配置反向代理到后端服务器&…

【操作系统面试题(32道)与面试Linux命令大全】

文章目录 操作系统面试题引论1.什么是操作系统&#xff1f;2.操作系统主要有哪些功能&#xff1f; 操作系统结构3.什么是内核&#xff1f;4.什么是用户态和内核态&#xff1f;5.用户态和内核态是如何切换的&#xff1f; 进程和线程6.并行和并发有什么区别&#xff1f;7.什么是进…

小白学安全-KunLun-M静态白盒扫描工具

一、KunLun-M简介 KunLun-M是一个完全开源的静态白盒扫描工具&#xff0c;支持PHP、JavaScript的语义扫描&#xff0c;基础安全、组件安全扫描&#xff0c;Chrome Ext\Solidity的基础扫描。开源地址&#xff1a;https://github.com/LoRexxar/Kunlun-M Cobra是一款源代码安全审计…

数据拟合、参数估计、插值等数据处理算法

介绍 数据拟合&#xff1a; 数据拟合是通过选择或构建合适的函数模型&#xff0c;将给定的数据点与该函数模型进行匹配和拟合的过程。常见的数据拟合方法包括最小二乘法和非线性最小二乘法。最小二乘法通过最小化实际数据与拟合函数的残差平方和来求解最优拟合参数。非线性最小…

Python之函数进阶-nonlocal和LEGB

Python之函数进阶-nonlocal和LEGB nonlocal语句 nonlocal:将变量标记为不在本地作用域定义&#xff0c;而是在上级的某一级局部作用域中定义&#xff0c;但不能是全局作用域中定义。 函数的销毁 定义一个函数就是生成一个函数对象&#xff0c;函数名指向的就是函数对象。可…

axios请求的问题

本来不想记录&#xff0c;但是实在没有办法&#xff0c;因为总是会出现post请求&#xff0c;后台接收不到数据的情况,还是记录一下如何的解决的比较好。 但是我使用export const addPsiPurOrder data > request.post(/psi/psiPurOrder/add, data); 下面是封装的代码。后台接…

幼教早教内容付费预约小程序的效果如何

很多家庭对孩子的重视程度很高&#xff0c;尤其加之如今激烈竞争的市场&#xff0c;孩子从小便需要各种提前教育&#xff0c;而相关教培企业也比较多&#xff0c;基于服务高需求度&#xff0c;线下教育与线上课程教育同样重要。 在实际经营中&#xff0c;幼教早教培训机构也面…