STL常用容器3
set和multiset容器
基本概念
简介:
所有元素都会在插入时自动排序
本质:
set/multiset 属于关联式容器,底层结构是用二叉树实现的
set和multiset的区别:
- set不允许有重复的元素
- multiset允许有重复的元素
set的构造与赋值
构造:
- set st; //默认构造函数
- set(const set &st) //拷贝构造函数
赋值
- set& operator=(const set &st); //重载等号运算符
#include<iostream>
using namespace std;
#include<set>
//set的构造函数与赋值
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin();it != s.end();it++) {
cout << *it << " ";
}
cout << endl;
}
void test01() {
//默认构造
set<int> s;
//插入数据只有insert()方式
s.insert(10);
s.insert(30);
s.insert(50);
s.insert(40);
s.insert(10);
//set中不允许有重复的元素,set在元素插入时,对其进行排序操作,默认升序
printSet(s);//10 30 40 50
//拷贝构造
set<int> s2(s);
printSet(s2);//10 30 40 50
set<int> s3 = s2;
printSet(s3);//10 30 40 50
}
int main() {
test01();
system("pause");
return 0;
}
set容器的大小和交换
函数原型
- size(); //返回元素的数目
- empty(); //判断容器是否为空
- swap(st); //交换2个集合容器
#include<iostream>
using namespace std;
#include<set>
//set的构造函数与赋值
//set的大小和交换
void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin();it != s.end();it++) {
cout << *it << " ";
}
cout << endl;
}
void test02() {
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(50);
s1.insert(40);
s1.insert(10);
if (!s1.empty()) {
cout << "s1不为空" << endl;
printSet(s1);//10 30 40 50
cout << "s1容器的元素数目" << s1.size() << endl;
}
else {
cout << "s1为空" << endl;
}
set<int> s2;
s2.insert(1);
s2.insert(3);
s2.insert(5);
s2.insert(4);
s2.insert(1);
cout << "交换前" << endl;
printSet(s1);//10 30 40 50
printSet(s2);//1 3 4 5
s1.swap(s2);
cout << "交换后" << endl;
printSet(s1);//1 3 4 5
printSet(s2);//10 30 40 50
}
int main() {
test02();
system("pause");
return 0;
}
set容器的插入与删除
函数原型
- insert(elem); //在容器中插入元素elem
- clear(); //清除容器中所有元素
- erase(pos); //删除pos位置上的元素,返回下一个元素的迭代器
- erase(beg,end); //删除区间[ beg,end ) 的元素,返回下一个元素的迭代器
- erase(elem); //删除容器中元素为elem的元素
#include<iostream>
using namespace std;
#include<set>
//- insert(elem); //在容器中插入元素elem
//-clear(); //清除容器中所有元素
//-erase(pos); //删除pos位置上的元素,返回下一个元素的迭代器
//-erase(beg, end); //删除区间[ beg,end ) 的元素,返回下一个元素的迭代器
//-erase(elem); //删除容器中元素为elem的元素
void printSet(set<int> &st) {
for (set<int>::iterator it = st.begin();it != st.end();it++) {
cout << *it << " ";
}
cout << endl;
}
void test01() {
set<int> s1;
//插入:
s1.insert(60);
s1.insert(20);
s1.insert(20);
s1.insert(50);
s1.insert(30);
printSet(s1);// 20 30 50 60
//擦除
s1.erase(s1.begin());
printSet(s1);// 30 50 60
s1.erase(50);
printSet(s1);
s1.erase(s1.begin(), --s1.end());
printSet(s1);
//清除
s1.insert(20);
s1.insert(200);
printSet(s1);
s1.clear();
printSet(s1);
}
int main() {
test01();
system("pause");
return 0;
}
set容器的查找与统计
函数原型
- find(key); //查找key是否存在,如果存在返回该键对应的元素的迭代器;如果不存在,返回set.end();
- cout(key); //统计key的对应元素的个数
#include<iostream>
using namespace std;
#include<set>
//- find(key); //查找key是否存在,如果存在返回该键对应的元素的迭代器;如果不存在,返回set.end();
//- cout(key); //统计key的对应元素的个数
void test02() {
set<int> s1;
s1.insert(60);
s1.insert(20);
s1.insert(20);
s1.insert(50);
s1.insert(30);
set<int>::iterator it =s1.find(30);
if (it != s1.end()) { //无需担心查找的元素是最后元素的位置,因为end()指向的是最后元素的下一个位置
cout << "找到元素" << endl;
}
else {
cout << "未找到元素" << endl;
}
int num = s1.count(30);
//对于set而言统计元素的个数要么是0要么是1
cout << "30在容器s1个个数为" << num << endl;
}
int main() {
test02();
system("pause");
return 0;
}
set和multiset的区别
区别:
- set不可以插入重复的元素,multiset可以插入重复的元素
- set插入元素的同时会返回插入结界,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
#include<iostream>
using namespace std;
#include<set>
//set和multiset区别
void test01() {
set<int> s1;
pair<set<int>::iterator, bool> res = s1.insert(10);
if (res.second) {
cout << "第一次插入成功" << endl;
}
else {
cout << "第一次插入失败" << endl;
}
res = s1.insert(10);
if (res.second) {
cout << "第二次插入成功" << endl;
}
else {
cout << "第二次插入失败" << endl;
}
multiset<int> s2;
//允许插入重复值,返回值是一个迭代器
s2.insert(10);
s2.insert(10);
cout << "容器s2的元素数目" << s2.size() << endl;//2
int num1=s1.count(10);
int num2=s2.count(10);
cout << "容器s1中元素10的数目为" << num1 << endl;//..... 1
cout << "容器s2中元素10的数目为" << num2 << endl;//..... 2
}
int main() {
test01();
system("pause");
return 0;
}
pair对组创建
功能描述:
成对出现的数据,利用队组可以返回两个数据
两种创建方式:
- pair<type,type> p( value1,value2);
- pair<type,type> p = make_pair(value1,value2);
#include<iostream>
using namespace std;
#include<string>
//pair队组的创建
//1. pair<type, type> p(value1, value2);
//2. pair<type, type> p = make_pair(value1, value2);
void test01() {
pair<string, int> p("张三", 18);
cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
pair<string, int> p2 = make_pair("里斯", 34);
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
int main() {
test01();
system("pause");
return 0;
}
set容器的排序
学习目标:
set容器默认是升序排序,掌握如何改变排序的规则
主要技术点:
利用仿函数,可以改变排序规则
#include<iostream>
using namespace std;
#include<set>
#include<string>
class MyCompare {
public:
bool operator()(int v1,int v2) const {//新版后面加const修饰,旧版可能不需要,表示在传入过程中不做修改
return v1 > v2;
}
};
//内置数据类型
void test01() {
set<int,MyCompare> s1;
s1.insert(10);
s1.insert(20);
s1.insert(50);
s1.insert(30);
for ( set<int, MyCompare>::iterator it = s1.begin();it != s1.end();it++) {
cout << *it << " ";
}
cout << endl;
}
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//自定义数据类型,需要自己指定排序规则
class MyCompare2 {
public:
bool operator()(Person p1, Person p2) const {
return p1.m_Age > p2.m_Age;
}
};
void test02() {
set<Person,MyCompare2> s1;
Person p1("张三", 32);
Person p2("关羽", 42);
Person p3("刘备", 50);
Person p4("赵云", 22);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
for (set<Person, MyCompare2>::iterator it = s1.begin();it != s1.end();it++) {
cout << "姓名: " << (*it).m_Name << " 年龄:" << (*it).m_Age << endl;
}
}
int main() {
test01();
test02();
system("pause");
return 0;
}
map和multimap容器
基本概念
简介:
- map中所有的元素都是pair
- pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
- 所有元素都会根据元素的键值自动排序
本质:
map和multimap属于关联式容器,底层结构是用二叉树实现的
优点:
可以根据key值快速找到value值
map和multimap的区别:
map不允许容器中有重复key值元素
multimap允许容器中有重复的key值元素
map构造和赋值
map构造
- map<T1,T2> mp; //map默认构造函数
- map(const map &mp); //map拷贝构造
赋值
map &operator=(const map &mp); //重载等号运算符
#include<iostream>
using namespace std;
#include<map>
//map的构造与赋值
// map构造
//-map<T1, T2> mp; //map默认构造函数
//-map(const map & mp); //map拷贝构造
//赋值
//map& operator=(const map& mp); //重载等号运算符
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin();it != m.end();it++) {
cout << (*it).first << " " << (*it).second << endl;
}
cout << endl;
}
void test01() {
//默认构造
map<int, int> mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(pair<int, int>(3, 20));
mp.insert(pair<int, int>(4, 30));
mp.insert(pair<int, int>(2, 40));
printMap(mp); //通过key自动排序,默认升序
//拷贝构造
map<int,int> m2(mp);
printMap(m2);
//赋值
map<int, int> m3;
m3 = m2;
printMap(m3);
}
int main() {
test01();
system("pause");
return 0;
}
map大小和交换
函数原型
- size(); //返回容器中的数目
- empty(); //判断容器是否为空
- swap(map); //交换两个map容器
#include<iostream>
using namespace std;
#include<map>
//map的大小和交换
//- size(); //返回容器中的数目
//- empty(); //判断容器是否为空
//- swap(map); //交换两个map容器
void printMap(map<int, int>& m) {
for (map<int, int>::iterator it = m.begin();it != m.end();it++) {
cout << "key="<<(*it).first << " value=" << (*it).second << endl;
}
cout << endl;
}
void test01() {
//默认构造
map<int, int> mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(pair<int, int>(3, 20));
mp.insert(pair<int, int>(4, 30));
mp.insert(pair<int, int>(2, 40));
if (!mp.empty()) {
cout << "容器mp不为空" << endl;
cout << "容器的大小为:" << mp.size();
}
else {
cout << "容器为空" << endl;
}
map<int, int> m1;
m1.insert(pair<int, int>(1, 1));
m1.insert(pair<int, int>(3, 2));
m1.insert(pair<int, int>(4, 3));
m1.insert(pair<int, int>(2, 4));
cout << "交换前:" << endl;
printMap(mp);
printMap(m1);
cout << "交换后: " << endl;
mp.swap(m1);
printMap(mp);
printMap(m1);
}
int main() {
test01();
system("pause");
return 0;
}
map插入和删除
函数原型
- insert(elem); //在容器中插入元素
- clear(); //清除所有元素
- erase(pos); //清除pos位置的元素,返回下一个位置的迭代器
- erase(beg,end); //清除[ beg,end )区间的元素,返回下一个位置的迭代器
- erase(key); //删除容器中值为key的元素
#include<iostream>
using namespace std;
#include<map>
//map的插入与删除
//- insert(elem); //在容器中插入元素
//-clear(); //清除所有元素
//-erase(pos); //清除pos位置的元素,返回下一个位置的迭代器
//-erase(beg, end); //清除[ beg,end )区间的元素,返回下一个位置的迭代器
//-erase(key); //删除容器中值为key的元素
void printMap(map<int, int>& m) {
for (map<int, int>::iterator it = m.begin();it != m.end();it++) {
cout << "key=" << (*it).first << " value=" << (*it).second << endl;
}
cout << endl;
}
void test01() {
map<int, int> mp;
//插入insert有3种参数写法
mp.insert(pair<int, int>(1, 10));//1
mp.insert(make_pair(3, 20));//2
mp.insert(map<int, int>::value_type(4, 30));//3
mp[2] = 40;//不建议用[]插入,且在输入时如果key值之前并没有插入,会默认创建一个,值为对应类型的默认值,容易误插
printMap(mp);
//清除
mp.erase(++mp.begin());
printMap(mp);
mp.erase(++mp.begin(), --mp.end());
printMap(mp);
mp.erase(1);//按照key删除
printMap(mp);
mp.clear();
printMap(mp);
}
int main() {
test01();
system("pause");
return 0;
}
map容器查找和统计
函数原型
- find(key); //查找key是否存在,如果存在,返回该键的元素的迭代器,如果不存在,返回set.end()
- cout(key); //统计key的元素个数
#include<iostream>
using namespace std;
#include<map>
//map的查找和统计
//- find(key); //查找key是否存在,如果存在,返回该键的元素的迭代器,如果不存在,返回set.end()
//-cout(key); //统计key的元素个数
void printMap(map<int, int>& m) {
for (map<int, int>::iterator it = m.begin();it != m.end();it++) {
cout << "key=" << (*it).first << " value=" << (*it).second << endl;
}
cout << endl;
}
void test01() {
map<int, int> mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(make_pair(3, 20));
mp.insert(map<int, int>::value_type(4, 30));
mp[2] = 40;
map<int, int>::iterator it = mp.find(2);
if (it != mp.end()) {
cout << "查到了元素的key值为: " << (*it).first << " 元素的value值为: " << (*it).second << endl;
}
else {
cout << "你好像没有插入过这个key的对组" << endl;
}
it = mp.find(5);
if (it != mp.end()) {
cout << "查到了元素的key值为 " << (*it).first << " 元素的value值为: " << (*it).second << endl;
}
else {
cout << "你好像没有插入过这个key的对组" << endl;
}
//map的统计值只有0和1
int num1=mp.count(1);
int num2=mp.count(6);
cout << num1 << " " << num2 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
map排序
map默认时升序排序的,利用仿函数可以改变map的排序规则
#include<iostream>
using namespace std;
#include<map>
class MyCompare {
public:
bool operator()(int v1, int v2) const {
return v1 > v2;
}
};
//map的排序
void printMap(map<int, int, MyCompare>& m) {
for (map<int, int, MyCompare>::iterator it = m.begin();it != m.end();it++) {
cout << "key=" << (*it).first << " value=" << (*it).second << endl;
}
cout << endl;
}
void test01() {
map<int, int,MyCompare> mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(make_pair(3, 20));
mp.insert(map<int, int>::value_type(4, 30));
mp[2] = 40;
printMap(mp);
}
int main() {
test01();
system("pause");
return 0;
}
STL案例二 员工分组
案例描述
- 招聘了10名员工(ABCDEFGHIJ),需要指派在哪个部门工作
- 员工信息分为:姓名,工资组成 部门分为:策划,美术,研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入:key(部门编号),value(员工)
- 分部门显示员工信息
实现步骤
- 创建10名员工放入vector中
- 遍历vector容器,取出每名员工,进行随机分组
- 分组后,将每个员工部门编号作为key,具体员工作为value,放入到multimap容器中
- 分部门显示员工信息
#include<iostream>
using namespace std;
#include<vector>
#include<map>
#include<string>
#include<ctime>
#define MEISHU 1
#define CEHUA 2
#define YANFA 3
//1. 创建10名员工放入vector中
//2. 遍历vector容器,取出每名员工,进行随机分组
//3. 分组后,将每个员工部门编号作为key, 具体员工作为value, 放入到multimap容器中
//4. 分部门显示员工信息
class Employee {
public:
Employee(string name, int wages) {
this->m_Name = name;
this->m_Wages = wages;
}
string m_Name;
int m_Wages;
};
//创建员工
void createEmployee(vector<Employee> &v) {
for (int i = 0;i < 10;i++) {
string nameArry ="ABCDEFGHIJ";
string name = "员工";
name += nameArry[i];
int wages = rand() % 4000 + 6000;
Employee eml(name, wages);
v.push_back(eml);
}
}
//分组
void groupEmployee(vector<Employee>& v, multimap<int, Employee>&m) {
for (vector<Employee>::iterator it = v.begin();it != v.end();it++) {
int deptNum = rand() % 3 + 1;
m.insert(make_pair(deptNum, *it));
}
}
//分部门显示
void showEmployee(multimap<int, Employee>& m) {
multimap<int, Employee>::iterator res1 = m.find(MEISHU);
multimap<int, Employee>::iterator res2 = m.find(CEHUA);
multimap<int, Employee>::iterator res3 = m.find(YANFA);
int num1 = m.count(MEISHU);
int num2 = m.count(CEHUA);
int num3 = m.count(YANFA);
cout << "美术部门:" << endl;
for (int num = 0;res1!=m.end()&&num < num1;res1++, num++) {
cout << "员工所属部门编号 " << (*res1).first << " " << (*res1).second.m_Name << " 薪资为" << (*res1).second.m_Wages << endl;
}
cout << "策划部门:" << endl;
for (int num = 0;res2 != m.end()&&num < num2;res2++, num++) {
cout << "员工所属部门编号 " << (*res2).first << " " << (*res2).second.m_Name << " 薪资为" << (*res2).second.m_Wages << endl;
}
cout << "研发部门:" << endl;
for (int num = 0;res3 != m.end()&&num < num3;res3++, num++) {
cout << "员工所属部门编号 " << (*res3).first << " " << (*res3).second.m_Name << " 薪资为" << (*res3).second.m_Wages << endl;
}
}
int main() {
srand((unsigned int)time(NULL));
vector<Employee> v;
multimap<int, Employee> mEmployee;
createEmployee(v);
groupEmployee(v, mEmployee);
showEmployee(mEmployee);
system("pause");
return 0;
}