set容器 Posted on 2020-04-22 | In STL Words count in article: 1.2k | Reading time ≈ 6 set容器 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218//// Created by Jason on 2020/3/29.///*3721set构造函数 set<T>st;//set默认构造函数: multiset<T>mst;// mit iset默认构造函数: set( const set&st);//拷考贝构造函数3722set赋值操作 set& operator=(const set&*st); swap(st);//交换两个集合容器3723set大小操作 size();/返回容器中元素的数目 empty();//判断容器是否为空3724set插入和删除操作 insert(elem)://在容器中插入元素。 clear();//除所有元素 erase(pos);//!剛除pos迭代器所指的元素,返回下一个元素的送代器 erase(beg,end);//刪除区间[beg,end]的所有元素,返回下一个元素的送代器。372.5set查找操作 find (key):/查找键key是存在,若存在,返回该键的元素的迭代器;若不存在,返回 set.end(); count (key):/查找键key的元素个数 lower_ bound (keyl1em):/回第一个key>=keyE1em元素的迭代器。 upper_bound(KeyElet):/回第一个key>keyE1em元素的迭代器。 equal_range (keyl1em):/回容器中key与keyE1em相等的上下限的两个迭代器。 * */#include<bits/stdc++.h>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> s; s.insert(10); s.insert(20); s.insert(30); s.insert(60); s.insert(50); s.insert(30); printSet(s); //set 特点:自动排序,重复的数值计算一次 s.erase(20); printSet(s);}void test02(){ set<int> s; s.insert(10); s.insert(20); s.insert(30); s.insert(30); s.insert(60); s.insert(50); s.find(30); set<int>::iterator pos = s.find(300); if(pos!= s.end()) { cout<<"找到了!:"<<*pos<<endl; } else { cout<<"找不到"<<endl; } set<int>::iterator pos2 = s.find(3000); if(pos2!= s.end()) { cout<<"找到了!:"<<*pos2<<endl; } else { cout<<"找不到"<<endl; } printSet(s); //set.count只允许出现0或1,因为同一个数字最多一次 int num = s.count(10 ); cout<<"10的个数为"<<num<<endl; //lower_bound(keyElem)返回第一个k>=keyElem 元素的迭代器 set<int>::iterator res= s.lower_bound(29); if(res!= s.end()) { cout<<"找到lowerbound值为"<<*res<<endl; } else{ cout<<"NOT FOUND"<<endl; }// upper_bound(KeyElet);/回第一个key>keyE1em元素的迭代器。 set<int>::iterator res2= s.upper_bound(30); if(res2!= s.end()) { cout<<"找到lowerbound值为"<<*res2<<endl; } else{ cout<<"NOT FOUND"<<endl; }// equal_range (keyl1em):/回容器中key与keyE1em相等的上下限的两个迭代器。 pair< set<int>::iterator ,set<int>::iterator >it=s.equal_range (30); if(it.first!=s.end()) { cout<<"找到equal_range中的lower_bound值为"<<*(it.first)<<endl; } else{cout<<"NOT FOUND"<<endl;} if(it.second!=s.end()) { cout<<"找到equal_range中的upper_bound值为"<<*(it.second)<<endl; } else{cout<<"NOT FOUND"<<endl;}}/*对组的声明方式*/void test03(){ pair<string,int>p(string("Tom"),18); cout<<"NAME IS "<<p.first<<" AGE IS "<<p.second<<endl; //第二种声明 pair<string,int> p2= make_pair("Jerry",29); cout<<"NAME IS "<<p2.first<<" AGE IS "<<p2.second<<endl;}void test04() { set<int> s; pair<set<int>::iterator, bool> ret = s.insert(10); if (ret.second) cout << "SUCCESSFULLY INSERT!" << endl; else cout << "UNSUCCESSFULLY INSERT!" << endl; ret = s.insert(10); if (ret.second) cout << "SUCCESSFULLY INSERT!" << endl; else cout << "UNSUCCESSFULLY INSERT!" << endl; multiset<int> ms; ms.insert(10); ms.insert(10); for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) { cout<<*it<<endl; } /*multiset 插入的元素是不管有几个相同的,都会打印出来*/}//利用仿函数,指定set容器的排序规则class MyCompare{public: bool operator()(int v1,int v2) { return v1>v2; }};void test05(){ set<int>s; s.insert(10); s.insert(50); s.insert(30); s.insert(40); s.insert(60); /*默认排序是从小到大!*/ printSet(s); /*插入之后无法改变,但是可以再插入之前指定排序规则*/ set<int,MyCompare>s2;//写了仿函数后,在set建立时要写入 s2.insert(10); s2.insert(50); s2.insert(30); s2.insert(40); s2.insert(60); cout<<endl; for(set<int,MyCompare>::iterator it = s2.begin();it!= s2.end();it++) { cout<<*it<<" "; }}class Person{public: Person(string name ,int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age;};class MycomparePerson{public: bool operator()(const Person &p1,const Person &p2) { //年龄升序 return p1.m_Age<p2.m_Age; }};void test06(){ set<Person,MycomparePerson> s; Person p1("aaa",10); Person p2("bbb",20); Person p3("vvv",30); Person p4("ccc", 70); Person p5("fff",50); Person p6("www",60); s.insert(p1); s.insert(p2); s.insert(p3); s.insert(p4); s.insert(p5); s.insert(p6); for(set<Person,MycomparePerson>::iterator it = s.begin();it!=s.end();it++) { cout<<"NAME IS " << (*it).m_Name<<" AGE IS "<< it->m_Age<<endl; }//不写仿函数,是打印不出来顺序排列的的!}int main(){// test01();// test02();// test03();// test04();// test05() ; test06();} -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/04/22/set%E5%AE%B9%E5%99%A8/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.