C++ STL set/multiset容器
set基本概念:
所有元素都会在插入时自动被排序
本质:
set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别:
set不允许容器中有重复的元素
multiset允许容器中有重复的元素
set构造和赋值
1 #include <iostream> 2 #include <string> 3 #include <set> 4 using namespace std; 5 //创建set容器以及赋值 6 /* 7 set<T> st; //默认构造函数 8 set(const set& st); //拷贝构造函数 9 set& operator=(const set& st); //重载等号操作符 10 */ 11 void printSet(const set<int>& s) 12 { 13 for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) 14 { 15 cout << *it << " "; 16 } 17 cout << endl; 18 } 19 void test() 20 { 21 set<int>s1; 22 //插入数据 只有insert方式 23 s1.insert(10); 24 s1.insert(40); 25 s1.insert(30); 26 s1.insert(20); 27 s1.insert(30); 28 //遍历容器 29 //set容器特点: 所有元素插入时候自动被排序 30 //set容器不允许插入重复值 31 printSet(s1);//10 20 30 40 32 set<int>s2(s1);//拷贝构造 33 printSet(s2);//10 20 30 40 34 set<int>s3; 35 s3 = s2; 36 printSet(s3);//10 20 30 40 37 } 38 int main() 39 { 40 test(); 41 system("pause"); 42 return 0; 43 }
set大小和交换
1 #include <iostream> 2 #include <string> 3 #include <set> 4 using namespace std; 5 //统计set容器大小以及交换set容器 6 /* 7 size(); //返回容器中元素的数目 8 empty(); //判断容器是否为空 9 swap(st); //交换两个集合容器 10 不允许重复数值 不能重新指定大小 11 */ 12 void printSet(const set<int>& s) 13 { 14 for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) 15 { 16 cout << *it << " "; 17 } 18 cout << endl; 19 } 20 //大小 21 void test() 22 { 23 set<int>s1; 24 s1.insert(10); 25 s1.insert(50); 26 s1.insert(30); 27 s1.insert(20); 28 printSet(s1);// 10 20 30 50 29 //判断是否为空 30 if (s1.empty()) 31 { 32 cout << "s1为空" << endl; 33 } 34 else 35 { 36 cout << "s1不为空" << endl; 37 cout << "s1的大小:" << s1.size() << endl; 38 } 39 } 40 //交换 41 void test2() 42 { 43 set<int>s1; 44 s1.insert(10); 45 s1.insert(50); 46 set<int>s2; 47 s2.insert(100); 48 s2.insert(200); 49 cout << "交换前:" << endl; 50 printSet(s1);//10 50 51 printSet(s2);//100 200 52 s1.swap(s2); 53 cout << "交换后:" << endl; 54 printSet(s1);//100 200 55 printSet(s2);//10 50 56 } 57 int main() 58 { 59 //test(); 60 test2(); 61 system("pause"); 62 return 0; 63 }
set插入和删除
1 #include <iostream> 2 #include <string> 3 #include <set> 4 using namespace std; 5 //set容器 插入 和 删除 6 /* 7 insert(elem); //在容器中插入元素 8 clear(); //清除所有元素 9 erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器 10 erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器 11 erase(elem); //删除容器中值为elem的元素 12 */ 13 void printSet(const set<int>& s) 14 { 15 for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) 16 { 17 cout << *it << " "; 18 } 19 cout << endl; 20 } 21 void test() 22 { 23 set<int>s1; 24 s1.insert(40); 25 s1.insert(10); 26 s1.insert(30); 27 s1.insert(20); 28 printSet(s1);//10 20 30 40 29 //删除 30 s1.erase(s1.begin());//删除第一个元素:删除最小值 31 printSet(s1);//20 30 40 32 //删除重载版本 33 s1.erase(30); 34 printSet(s1);//20 40 35 //清空 36 s1.erase(s1.begin(), s1.end()); 37 s1.clear(); 38 } 39 int main() 40 { 41 test(); 42 system("pause"); 43 return 0; 44 }
set查找和统计
1 #include <iostream> 2 #include <string> 3 #include <set> 4 using namespace std; 5 //set查找 和 统计 6 /* 7 find(key); //查找key是否存在,若存在,返回该键的元素的迭代器; 若不存在,返回set.end(); 8 count(key); //统计key的元素个数 9 */ 10 void printSet(const set<int>& s) 11 { 12 for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) 13 { 14 cout << *it << " "; 15 } 16 cout << endl; 17 } 18 void test() 19 { 20 set<int>s1; 21 //插入数据 22 s1.insert(10); 23 s1.insert(40); 24 s1.insert(20); 25 s1.insert(30); 26 //查找 27 set<int>::iterator pos = s1.find(30); 28 if (pos != s1.end())//找到元素 29 { 30 cout << "找到元素: " << *pos << endl; 31 } 32 else 33 { 34 cout << "未找到元素" << endl; 35 } 36 } 37 void test2() 38 { 39 set<int> s1; 40 s1.insert(10); 41 s1.insert(30); 42 s1.insert(30); 43 s1.insert(30); 44 int num = s1.count(30); 45 //对于set而言,统计结果只是为0 或 1 46 cout << "num = " << num << endl; 47 } 48 int main() 49 { 50 //test(); 51 test2(); 52 system("pause"); 53 return 0; 54 }
set和multiset区别
1 #include <iostream> 2 #include <string> 3 #include <set> 4 using namespace std; 5 //set 和 multiset 区别 6 /* 7 set不可以插入重复数据,而 multiset可以 8 set插入数据的同时会返回插入结果,表示插入是否成功 9 multiset不会检测数据,因此可以插入重复数据 10 */11 void test() 12 { 13 set<int>s; 14 //set返回 _Pairib类型 = pair<iterator,bool> 15 pair<set<int>::iterator, bool> ret = s.insert(10); 16 if (ret.second)//返回值的第二个数据 17 { 18 cout << "第一次插入成功" << endl; 19 } 20 else 21 { 22 cout << "第一次插入失败" << endl; 23 } 24 ret = s.insert(10); 25 if (ret.second)//返回值的第二个数据 26 { 27 cout << "第二次插入成功" << endl; 28 } 29 else 30 { 31 cout << "第二次插入失败" << endl; 32 } 33 multiset<int>ms; 34 //允许插入重复值 35 ms.insert(10);//multiset返回迭代器 iterator 36 ms.insert(10); 37 ms.insert(10); 38 ms.insert(10); 39 for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) 40 { 41 cout << *it << " " ; 42 } 43 cout << endl; 44 } 45 int main() 46 { 47 test(); 48 system("pause"); 49 return 0; 50 }
pair对组创建
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 //成对出现的数据, 利用对组可以返回两个数据 5 /* 6 pair<type, type> p (value1, valu2); 7 pair<type, type> p = make_pair(value1, value2); 8 */ 9 void test() 10 { 11 //第一种方式 12 pair<string, int> p("tom",20); 13 cout << "姓名:" << p.first << "\t年龄:" << p.second << endl; 14 //第二种方式 15 pair<string, int>p2 = make_pair("jack", 40); 16 cout << "姓名:" << p2.first << "\t年龄:" << p2.second << endl; 17 } 18 int main() 19 { 20 test(); 21 system("pause"); 22 return 0; 23 }
set容器排序
1 #include <iostream> 2 #include <string> 3 #include <set> 4 using namespace std; 5 //set容器默认排序规则从小到大,掌握如何改变排序规则 6 /* 7 利用仿函数,可以改变排序规则 8 */ 9 class MyCompare 10 { 11 public: 12 bool operator()(int v1, int v2) const 13 { 14 return v1 > v2;//降序 15 } 16 }; 17 void test() 18 { 19 set<int>s1; 20 s1.insert(10); 21 s1.insert(30); 22 s1.insert(40); 23 s1.insert(20); 24 for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) 25 { 26 cout << *it << " "; 27 } 28 cout << endl; 29 //指定排序规则 从大到小 30 set<int,MyCompare>s2; 31 s2.insert(10); 32 s2.insert(30); 33 s2.insert(40); 34 s2.insert(20); 35 for (set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) 36 { 37 cout << *it << " "; 38 } 39 cout << endl; 40 } 41 int main() 42 { 43 test(); 44 system("pause"); 45 return 0; 46 }
自定义类型的排序方式:
1 #include <iostream> 2 #include <string> 3 #include <set> 4 using namespace std; 5 //set存放自定义数据类型 排序 6 class Person 7 { 8 public: 9 Person(string name, int age) 10 { 11 this->m_Name = name; 12 this->m_Age = age; 13 } 14 string m_Name; 15 int m_Age; 16 }; 17 class comparePerson 18 { 19 public: 20 bool operator()(const Person& p1,const Person& p2) const 21 { 22 //自定义规则 23 return p1.m_Age < p2.m_Age; 24 } 25 }; 26 void test() 27 { 28 //自定义数据类型 都会指定排序规则 29 set<Person,comparePerson>s; 30 Person p1("大", 11); 31 Person p2("二", 22); 32 Person p3("三", 33); 33 Person p4("四", 44); 34 s.insert(p1); 35 s.insert(p2); 36 s.insert(p3); 37 s.insert(p4); 38 for (set<Person,comparePerson>::iterator it = s.begin(); it != s.end(); it++) 39 { 40 cout <<"姓名: " << it->m_Name <<"\t年龄:" << it->m_Age << endl; 41 } 42 } 43 int main() 44 { 45 test(); 46 system("pause"); 47 return 0; 48 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)