Loading... ## set基本概念: ### 简介: 所以元素都会在插入是自动排序 ### 本质: set/multiset属于关联式容器,底层结构是用二叉树实现 ### set和multiset区别: * set不允许容器中有重复的元素 * multiset允许容器中有重复的元素 ## set构造和赋值: ### 功能描述: 创建set容器以及赋值 ### 构造: set< int>st;//默认构造函数 set(const set &st);//拷贝构造函数 ### 赋值: set& operator = (const set &st);//重载等号操作符 ### 示例: ```cpp #include<iostream> #include<set> using namespace std; void print_set(set<int>& st) { for (set<int>::iterator it = st.begin(); it != st.end(); it++) { cout << *it <<" "; } cout << endl; } void text() { set<int>st; st.insert(100); st.insert(20); st.insert(50); st.insert(80); st.insert(30); print_set(st); set<int>st1(st);//拷贝构造 print_set(st); set<int>st2; st2 = st;//赋值构造 print_set(st); } int main() { text(); return 0; } ``` ## set大小和交换 ### 功能描述: 统计set容器大小以及交换set容器 ### 函数原型: size();//返回容器中元素的数目 empty();//判断容器是否为空 ### 示例: ```cpp #include<iostream> #include<set> using namespace std; void print_set(const set<int>& st) { for (set<int>::const_iterator it = st.begin(); it != st.end(); it++) { cout << *it <<" "; } cout << endl; } void text() { set<int>st; st.insert(100); st.insert(20); st.insert(50); st.insert(80); st.insert(30); print_set(st); if (st.empty()) { cout << "容器为空!"<<endl; } else { cout << "r容器不为空!" << endl; cout << "容器大小为:" << st.size() << endl; } } void swap_set() { set<int>st1; st1.insert(100); st1.insert(20); st1.insert(50); st1.insert(80); st1.insert(30); set<int>st2; st2.insert(100); st2.insert(200); st2.insert(500); st2.insert(800); st2.insert(300); cout << "交换前:" << endl; print_set(st1); print_set(st2); cout << "交换后:" << endl; st1.swap(st2); print_set(st1); print_set(st2); } int main() { text(); swap_set(); return 0; } ``` ## set插入和删除 ### 功能描述: set容器进行插入数据和删除数据 ### 函数原型: insert(elem);//在容器中插入元素 clear();//清除所以元素 erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器 erase(elem);//删除容器中值为elem的元素 erase(beg, end);//删除区间(beg, end)的所以元素,返回下一个元素的迭代器 ### 示例: ```cpp #include<iostream> #include<set> using namespace std; void print_set(const set<int>& st) { for (set<int>::const_iterator it = st.begin(); it != st.end(); it++) { cout << *it <<" "; } cout << endl; } void text() { set<int>st; st.insert(100);//插入 st.insert(20); st.insert(50); st.insert(80); st.insert(30); print_set(st); st.erase(st.begin());//删除首项元素(注:是排序好的首项元素) print_set(st); st.erase(50);//删除数据为50的元素 print_set(st); st.clear();//清空容器 } int main() { text(); return 0; } ``` ## set查找和统计 ### 功能描述: 对set容器进行查找数据以及统计数据 ### 函数原型: find(key);//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end(); const(key);//统计key的元素个数 ### 示例: ```cpp #include<iostream> #include<set> using namespace std; void text() { set<int>st; st.insert(100);//插入 st.insert(20); st.insert(50); st.insert(30); set<int>::iterator it = st.find(50); if (it != st.end()) { cout << "容器找到元素 " << *it<<endl; } else { cout << "容器没找到元素" << endl; } int sum = st.count(30);//统计30的个数,要么是0,要么是1 cout << sum; } int main() { text(); return 0; } ``` ## set和multiset区别 set不可插入重复数据,而multiset可以 set插入数据的同时会返回插入结果,表示插入是否成功 multliset不会检测数据,因此可以插入重复数据 ### 示例: ```cpp #include<iostream> #include<set> using namespace std; void text() { set<int>st; pair<set<int>::iterator, bool> ret = st.insert(10);//set不可插入重发数据 if (ret.second) { cout << "插入成功!" << endl; } else { cout << "插入失败!" << endl; } ret = st.insert(10);//插入 if (ret.second) { cout << "插入成功!" << endl; } else { cout << "插入失败!" << endl; } multiset<int>mul;//multiset可插入重复数据 mul.insert(10); mul.insert(10); mul.insert(10); for (multiset<int>::iterator it = mul.begin(); it != mul.end(); it++) { cout << *it << " "; } cout << endl; } int main() { text(); return 0; } ``` ## pair对组创建 ### 功能描述: 成对出现的数据,利用对组可以返回两个数据 ### 两种创建方式: pair< type, type>p(value1, value2); pair< type, type>p = make_pair(value1, value2); ### 示例: ```cpp #include<iostream> #include<string> using namespace std; void text() { pair<string, string>p("本当迷,", "中秋节快乐!"); cout << p.first << p.second << endl<<endl; pair<string, string>p1; p1 = make_pair("祝家人们", "身体健康,万事如意,天天开心!"); cout << p1.first << p1.second << endl; } int main() { text(); return 0; } ``` ## set容器排序: ### 主要利用技术点: 利用仿函数,改变排序规则 ### 示例: ```cpp #include<iostream> #include<set> using namespace std; class Mycompare { public: bool operator()(int a, int b)const { return a > b; } }; void text() { //升序输出 set<int>p; p.insert(10); p.insert(50); p.insert(80); p.insert(30); p.insert(20); for (set<int>::iterator it = p.begin(); it != p.end(); it++) { cout << *it << " "; } cout << endl; //降序输出 set<int, Mycompare>p1; p1.insert(10); p1.insert(50); p1.insert(80); p1.insert(30); p1.insert(20); for (set<int, Mycompare>::iterator is = p1.begin(); is != p1.end(); is++) { cout << *is << " "; } cout << endl; } int main() { text(); return 0; } ``` ## set存放自定义数据类型 ```cpp #include<iostream> #include<set> #include<string> using namespace std; class Person { public: Person(const string name,const int age) { this->name = name; this->age = age; } public: string name; int age; }; class sorting { public: bool operator()(const Person& p1, const Person& p2)const { return p1.age > p2.age; } }; void text() { set<Person, sorting>p; Person p1("小明", 8); Person p2("小王", 7); Person p3("小李", 6); Person p4("小张", 10); Person p5("小杨", 2); p.insert(p1); p.insert(p2); p.insert(p3); p.insert(p4); p.insert(p5); for (set<Person, sorting>::iterator it = p.begin(); it != p.end(); it++) { cout << "名字:" << it->name << " 年龄:" << it->age << endl; } } int main() { text(); return 0; } ``` 最后修改:2021 年 09 月 20 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。