/*关联式容器共性:都是用二叉查找树实现的,都自动根据关键字排序
set<K>, multiset<K>, map<K,V>, multimap<K,V)
查找:.find(key)返回一个迭代器指向找到的第一个元素,失败返回.end()
统计:.count(key)统计关键字等于key的元素的个数
删除:.erase(key)删除关键字等于key的所有元素
区间:.lower_bound(key)取得关键字为key的第一个元素的位置, .upper_bound(key)取得关键字为key的最后一个元素之后的位置, .equal_range(key)一次取得关键字为key的元素的区间,返回一个pair
插入:.insert(element)
构造函数:可以用比较函数作为参数bool(*compare)(K a,K b)
*/
#include <set>
#include <iostream>
using namespace std;
#include "../stl2/print.h"
struct Person{
string name;
int age;
public:
Person(const char* n, int a):name(n),age(a){}
};
bool operator<(const Person& a, const Person& b)
{return a.age<b.age||a.age==b.age&&a.name<b.name;}
ostream& operator<<(ostream&o, const Person& x)
{return o<<x.name<<':'<<x.age;}
int main()
{
multiset<Person> mp;
mp.insert(Person("ZYP",26));
mp.insert(Person("GYR",18));
mp.insert(Person("YLB",20));
mp.insert(Person("ZYP",26));
mp.insert(Person("ZYP",26));
mp.insert(Person("WCB",21));
mp.insert(Person("GYR",18));
mp.insert(Person("YLB",20));
mp.insert(Person("ZHM",22));
mp.insert(Person("ZYP",26));
mp.insert(Person("GYR",18));
mp.insert(Person("WCB",21));
mp.insert(Person("GYR",18));
mp.insert(Person("WCB",21));
mp.insert(Person("GYR",18));
print(mp.begin(), mp.end());
multiset<Person>::iterator it=mp.find(Person("ZHM",22));
if(it==mp.end()) cout << "没有找到ZHM" << endl;
else cout << "发现目标:" << *it << endl;
it=mp.find(Person("FR",22));
if(it==mp.end()) cout << "没有找到FR" << endl;
else cout << "发现目标:" << *it << endl;
it = mp.find(Person("GYR",18));
cout << mp.count(*it) << "个" << *it << endl;
it = mp.find(Person("ZYP",26));
cout << mp.count(*it) << "个" << *it << endl;
multiset<Person>::iterator ib, ie;
ib = mp.lower_bound(Person("WCB",21));
ie = mp.upper_bound(Person("WCB",21));
cout << "===================" << endl;
print(ib, ie);
cout << "===================" << endl;
pair<multiset<Person>::iterator,multiset<Person>::iterator>p=mp.equal_range(Person("YLB",20));
print(p.first,p.second);
typedef multiset<Person>::iterator Iter;
pair<Iter,Iter>q=mp.equal_range(Person("GYR",18));
print(q.first,q.second);
cout << "===================" << endl;
mp.erase(Person("GYR",18));
mp.erase(Person("ZYP",26));
print(mp.begin(), mp.end());
}
/*运行结果:
GYR:18 GYR:18 GYR:18 GYR:18 GYR:18 YLB:20 YLB:20 WCB:21 WCB:21 WCB:21 ZHM:22 ZYP:26 ZYP:26 ZYP:26 ZYP:26
发现目标:ZHM:22
没有找到FR
5个GYR:18
4个ZYP:26
===================
WCB:21 WCB:21 WCB:21
===================
YLB:20 YLB:20
GYR:18 GYR:18 GYR:18 GYR:18 GYR:18
===================
YLB:20 YLB:20 WCB:21 WCB:21 WCB:21 ZHM:22
*/
c++ STL关联式容器的共性(权哥)
最新推荐文章于 2023-07-12 19:45:30 发布