构造和赋值
基本概念
简介:
本质:
- set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别:
- set不允许容器中有重复的元素
- multiset允许容器中有重复的元素
构造和赋值
功能描述:创建set容器以及赋值
构造:
1 2
| set<T> st; set(const set &st);
|
赋值:
1
| set& operator=(const set &st);
|
总结:
- set容器插入数据时用insert
- set容器插入数据的数据会自动排序
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include<iostream> #include<set> using namespace std;
void printSet(set<int>& s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { set<int>s1; s1.insert(10); s1.insert(40); s1.insert(30); s1.insert(20); s1.insert(30); printSet(s1); set<int>s2(s1); printSet(s2); set<int>s3; s3 = s2; printSet(s3); } int main() { test01(); system("pause"); return 0; }
|
运行结果
1 2 3
| 10 20 30 40 10 20 30 40 10 20 30 40
|
大小和交换
功能描述:
函数原型:
1 2 3
| size(); empty(); swap(st);
|
总结:
- 统计大小–size
- 判断是否为空–empty
- 交换容器–swap
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<iostream> #include<set> using namespace std;
void printSet(set<int>& s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; }
void test01() { set<int>s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); printSet(s1); if (s1.empty()) { cout << "s1为空" << endl; } else { cout << "s1不为空" << endl; cout << "s1的大小为:" << s1.size() << endl; } }
void test02() { set<int>s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); set<int>s2; s2.insert(100); s2.insert(300); s2.insert(200); s2.insert(400); cout << "交换前:" << endl; printSet(s1); printSet(s2); cout << "交换后:" << endl; s1.swap(s2); printSet(s1); printSet(s2); } int main() { test01(); test02(); system("pause"); return 0; }
|
运行结果
1 2 3 4 5 6 7 8 9
| 10 20 30 40 s1不为空 s1的大小为:4 交换前: 10 20 30 40 100 200 300 400 交换后: 100 200 300 400 10 20 30 40
|
插入和删除
功能描述:
函数原型:
1 2 3 4 5
| insert(elem); clear(); erase(pos); erase(beg, end); erase(elem);
|
总结:
- 插入—insert
- 删除—erase
- 清空—clear
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include<iostream> #include<set> using namespace std;
void printSet(set<int>& s) { for (set<int>::iterator it = s.begin(); it != s.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { set<int>s1; s1.insert(30); s1.insert(10); s1.insert(20); s1.insert(40); printSet(s1); s1.erase(s1.begin()); printSet(s1); s1.erase(30); printSet(s1); s1.erase(s1.begin(), s1.end()); s1.clear(); printSet(s1); } int main() { test01(); system("pause"); return 0; }
|
运行结果
1 2 3
| 10 20 30 40 20 30 40 20 40
|
查找和统计
功能描述:
函数原型:
总结
- 查找—find(返回的是迭代器)
- 统计—count(对于set,结果为0或者1)
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include<iostream> #include<set> using namespace std; void test01() { set<int>s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); set<int>::iterator pos = s1.find(30); if (pos != s1.end()) { cout << "找到元素:" << *pos << endl; } else { cout << "未找到元素" << endl; } }
void test02() { set<int>s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(40); int num = s1.count(30); cout << "num = " << num << endl; } int main() { test01(); test02(); system("pause"); return 0; }
|
运行结果
set和multiset区别
学习目标:
区别:
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
总结:
- 如果不允许插入重复数据可以利用set
- 如果需要插入重复数据利用multiset
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include<iostream> #include<set> using namespace std; void test01() { set<int>s; pair<set<int>::iterator, bool>ret = s.insert(10); if (ret.second) { cout << "第一次插入成功" << endl; } else { cout << "第一次插入失败" << endl; } ret = s.insert(10); if (ret.second) { cout << "第二次插入成功" << endl; } else { cout << "第二次插入失败" << endl; } multiset<int>ms; ms.insert(10); ms.insert(10); ms.insert(10); ms.insert(10); for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) { cout << *it << endl; } cout << endl; } int main() { test01(); system("pause"); return 0; }
|
运行结果
1 2 3 4 5 6
| 第一次插入成功 第二次插入失败 10 10 10 10
|
pair对组创建
功能描述:
两种创建方式:
1 2
| pair<type, type>p(value1, value2); pair<type, type>p=make_pair(value1,value2);
|
总结:
两种方式都可以创建对组,记住一种即可
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include<iostream> #include<set> using namespace std; void test01() { pair<string, int>p("Tom", 20); cout << "姓名:" << p.first << " 年龄:" << p.second << endl; pair<string, int>p2 = make_pair("Jerry", 30); cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl; } int main() { test01(); system("pause"); return 0; }
|
运行结果
1 2
| 姓名:Tom 年龄:20 姓名:Jerry 年龄:30
|
内置类型指定排序规则
学习目标:
- set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术点:
Demo1: set存放内置数据类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include<iostream> #include<set> using namespace std;
class MyCompare { public: bool operator()(int v1, int v2)const { return v1 > v2; } }; void test01() { set<int>s1; s1.insert(10); s1.insert(40); s1.insert(20); s1.insert(50); s1.insert(30); for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) { cout << *it << " "; } cout << endl; set<int, MyCompare>s2; s2.insert(10); s2.insert(40); s2.insert(20); s2.insert(50); s2.insert(30); for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) { cout << *it << " "; } cout << endl; } int main() { test01(); system("pause"); return 0; }
|
运行结果
1 2
| 10 20 30 40 50 50 40 30 20 10
|
总结:利用仿函数可以指定set容器的排序规则
Demo2:set存放自定义数据类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include<iostream> #include<set> using namespace std;
class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; class comparePerson { public: bool operator()(const Person& p1, const Person& p2)const { return p1.m_Age > p2.m_Age; } }; void test01() { set<Person, comparePerson>s; Person p1("刘备", 24); Person p2("关羽", 28); Person p3("张飞", 25); Person p4("赵云", 21); s.insert(p1); s.insert(p2); s.insert(p3); s.insert(p4); for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++) { cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl; } } int main() { test01(); system("pause"); return 0; }
|
运行结果
1 2 3 4
| 姓名:关羽 年龄:28 姓名:张飞 年龄:25 姓名:刘备 年龄:24 姓名:赵云 年龄:21
|
总结:对于自定义数据类型,set必须指定排序规则才可以插入数据