multiset和set同为关联容器,即容器内部元素为有序的,不同于vector等顺序容器中元素按插入顺序排列,两者的不同之处为multiset中可以包含相同元素,而set中不可以包含相同元素,下面列出对两个容器的一些基本操作。
multiset的一些基本操作。
#include<iostream>
#include<set>
using namespace std;
template <class T>
void Print(T first,T last){
for(;first!=last;++first) cout<<*first<<" ";
cout<<endl;
}
class A{
private:
int n;
public:
A(int n_){n=n_;}
friend bool operator< (const A & a1,const A & a2){
return a1.n<a2.n;
}
friend ostream & operator<< (ostream & o,const A & a2){
o<<a2.n;
return o;
}
friend class MyLess;
};
struct MyLess{
bool operator()(const A & a1,const A & a2){
//按个位数比较大小
return (a1.n%10)<(a2.n%10);
}
};
typedef multiset<A> MSET1; //MSET1用"<"比较大小
typedef multiset<A,MyLess>MSET2; //MSET2用MyLess::operator()比较大小
int main(){
const int SIZE=6;
A a[SIZE]={4,22,19,8,33,40};
MSET1 m1;
m1.insert(a,a+SIZE);
m1.insert(22);
cout<<"1)"<<m1.count(22)<<endl; //输出 1)2
cout<<"2)";Print(m1.begin(),m1.end()); //输出 2)4 8 19 22 22 33 40
//m1元素:4 8 19 22 22 33 40
MSET1::iterator pp=m1.find(19);
if(pp!=m1.end()){ //条件为真说明找到
cout<<"found"<<endl;
//本行会被执行,输出found
}
cout<<"3)";
cout<<*m1.lower_bound(22)<<","<<*m1.upper_bound(22)<<endl;
//输出3)22,33
m1.erase(m1.lower_bound(22),m1.upper_bound(22));
cout<<"4)";Print(m1.begin(),m1.end()); //输出4)4 8 19 33 40
cout<<"5)";cout<<*pp<<endl; //输出5)33
MSET2 m2; //m2里的元素按n的个位数从小到大排
m2.insert(a,a+SIZE);
cout<<"6)";Print(m2.begin(),m2.end()); //输出6)40 22 33 4 8 19
return 0;
}
set的一些基本操作
#include<iostream>
#include<set>
using namespace std;
int main(){
typedef set<int>::iterator IT;
int a[5]={3,4,6,1,2};
set<int> st(a,a+5); //st里是1 2 3 4 6
pair<IT,bool> result;
result=st.insert(5); //st变成1 2 3 4 5 6
if(result.second){ //插入成功则输出被插入元素
cout<<*result.first<<"inserted"<<endl; //输出:5inserted
}
if(st.insert(5).second) cout<<*result.first<<endl;
else{
cout<<*result.first<<"already exists"<<endl; //输出5 already exists
}
pair<IT,IT> bounds=st.equal_range(4);
cout<<*bounds.first<<","<<*bounds.second; //输出 :4 5
return 0;
}