集合set multiset
#include<set>
set、multiset都是集合类,差别在于set中不允许有重复元素,multiset中允许有重复元素。
multiset(const multiset& x) :复制构造函数 multiset<int> myset2(myset)
multiset(const value_type *first,const value_type *last,const Pred& comp=Pred(),const A& al=A()) :复制[first,last)之间元素构成新集合
例:multiset<int>myset3(a,a+sizeof(a)/sizeof(int));
int size() const :返回容器元素个数
bool empty() const :判断容器是否为空,若返回true,表明容器已空
pair<iterator,bool>insert(const value_type& x) :插入元素x myset.insert(a[i]) //其中a[i]是int型的, multiset<int> myset 即myset是 multiset的
iterator begin() :返回首元素的迭代器指针
iterator end() :返回尾元素后的迭代器指针,而不是尾元素的迭代器指针
(1) multiset集合默认是按升序排列的,这是由于源码中multiset模板参数中Pred=less<Key>决定了键值是按升序排列的
(2) multiset允许有重复元素,如果把multiset全部换成set,则结果没有重复元素,且值也按升序排列
#include <iostream>
#include<string>
#include<sets>
#include <map>
using namespace std;
class CEmployee //基类构造员工的基本信息
{string name;
string departname;
public:
CEmployee(string name,string departname)
{this->name=name;
this->departname=departname;
}
bool operator<(const CEmployee& e)const //按部门升序排列,再按姓名升序排列
{
bool mark=(departname.compare(e.departname)<0)?true:false;
if(departname.compare(e.departname)==0)
{
mark=(name.compare(e.name)<0)?true:false;
}
return mark;
}
string GetName(){return name;}
string GetDepart(){return departname;}
};
class CManage
{
multiset<CEmployee>myset;
public:
bool Add(CEmployee& e)
{
myset.insert(e);
return true;
}
void Show()
{
multiset<CEmployee>::iterator te=myset.begin();
while(te!=myset.end())
{
CEmployee& obj=*te;
cout<<obj.GetDepart<<"\t"<<obj.GetName<<endl;
te++;
}
}
};
int main()
{
CEmployee e1("zhangsan","人力部");
CEmployee e2("zhouqi","装配部");
CEmployee e3("wangwu","制造部");
CEmployee e4("zhaoliu","制造部");
CEmployee e5("lisi","装配部");
CEmployee e6("tianjiu","制造部");
CManage manage;
manage.Add(e1);
manage.Add(e2);
manage.Add(e3);
manage.Add(e4);
manage.Add(e5);
manage.Add(e6);
manage.Show();
return 0;
}