Set和multiset
头文件#include <set>
关联容器
Set,multiset,map,multimap
内部元素有序排列,新元素插入的位置取决于它的值,查找速度快。
除了各容器都有的函数外,还支持一下成员函数:
Find:查找等于某个值的元素(x小于y和y小于x同时不成立即为相等)
Lower_bound:查找某个下界
Upper_bound:查找某个上界
Equal_range:同时查找上界和下界
Count:计算等于某个值的元素的个数(x小于y和y小于x同时不成立即为相等)
Insert:用以插入一个元素或一个区间
Pair模板(STL预先定义的模板,map,multimap容器里放着的都是pair模板类的对象,且按照first从小到大排序)
参考代码
//pair模板
template<class _T1, class _T2>
struct pair{
typedef _T1 first_type;
typedef _T2 second_type;
_T1 first;
_T2 second;
//pair模板的构造函数
pair():first(),second(){
}
pair(const _T1 &_a,const _T2 &_b):first(_a),second(_b){
}
template<class _U1,class _U2>
pair(const pair< _U1, _U2> &_p ):first(_p.first),second(_p.second){
}
};
pair<int ,int >
p(pair<double,double>(5.5,4.6));
//p.first = 5, p.second = 4
Multiset的成员函数
1、Iterator find(const T& val);在容器中查找值为val的元素,返回其迭代器。如果找不到,返回end()。
2、iterator insert(constT & val) ; 将val插入到容器中并返回其迭代器。
3、Void insert(iteratorfirst, iterator last); 将区间[first, last)插入容器。
4、Int count(const T&val); 统计有多少个元素的值和val相等
5、Iterator lower_bound(const T&val);查找一个最大的位置it,使得[begin(), it)中所有的元素都比val小。
6、Iterator upper_bound(const T&val);查找一个最大的位置it,使得[it,end())中所有的元素都比val大。
7、Pair<iterator,iterator>equal_range(const T &val);同时求得lower_bound和upper_bound
8、iteratorerase(iterator it);删除it指向的元素,返回其后面的元素的迭代器。(vs2010)
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;
cout<<"2)";
Print(m1.begin(),m1.end());
MSET1::iterator pp=m1.find(19);
if(pp!=m1.end())
cout<<"found"<<endl;
cout<<"3)";cout<<*m1.lower_bound(22)<<","<<*m1.upper_bound(22)<<endl;
pp = m1.erase ( m1.lower_bound(22) , m1.upper_bound (22 ) );
cout<<"4)";Print(m1.begin(),m1.end());
cout<<"5)";cout<<*pp<<endl;
MSET2 m2;
m2.insert(a,a+SIZE);
cout<<"6)";Print(m2.begin(),m2.end());
return 0;
}
输出结果:
1)2
2)4,8,19,22,22,33,40
Found
3)22,33
4)4 8 19 33 40
5)33
6)40 22 33 4 8 19
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 5
pair<IT,bool> result;
result = st.insert(5); //st:1 2 3 4 5 6
if(result.second) //插入成功则输出被插入元素
cout<<*result.first<<"inserted"<<endl;
if(st.insert(5).second)
//插入第二个5,但是set中已经存在这个元素,走到else执行
cout<<*result.first<<endl;
else
cout<<*result.first<<"already exists"<<endl;
pair<IT,IT> bounds = st.equal_range(4);
//lower_bound 是first,upper_bound 是 second 返回值
cout<<*bounds.first<<","<<*bounds.second;
return 0;
}
输出结果:
5 inserted
5 already exists
4,5