C++篇——关联容器Set和MultiSet

关联容器set

1.关联容器内部是排好序的,排序的大小可以自己定义
2.关联容器除了之前共有的成员函数,还有以下的成员函数

  • find: 查找等于某个值 的元素(x小于y和y小于x同时不成立即为相等)
  • lower_bound : 查找某个下界
  • upper_bound : 查找某个上界
  • equal_range : 同时查找上界和下界
  • count :计算等于某个值的元素个数(x小于y和y小于x同时不成立即为相等)
  • insert: 用以插入一个元素或一个区间

pair模板

在学习关联容器之前,要学习一个预备知识:pair模板;
它是stl内预习定义的类模板,map/multimap容器里放着的都是pair模版类的对象,且按first从小到大排序。

template<class _T1, class _T2>
struct pair
{
typedef _T1 first_type;
typedef _T2 second_type;
_T1 first;
_T2 second;
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) { }
};

mutiset容器

template<class Key, class Pred = less<“Key”>,
class A = allocator<“Key”> >
class multiset { …… };
Pred类型的变量决定了multiset 中的元素,“一个比另一个小”是怎么定义的。
multiset运行过程中,比较两个元素x,y的大小的做法,就是生成一个 Pred类型的
变量,假定为 op,若表达式op(x,y) 返回值为true,则 x比y小。
Pred的缺省类型是 less<“Key”>。

multiset的成员函数

  • iterator find(const T & val); 在容器中查找值为val的元素,返回其迭代器。如果找不到,返回end()。
  • iterator insert(const T & val); 将val插入到容器中并返回其迭代器。 void insert(
  • iterator first,iterator last); 将区间[first,last)插入容器。 int count(const T& val); 统计有多少个元素的值和val相等。
  • iterator lower_bound(const T & val);查找一个最大的位置 it,使得[begin(),it) 中所有的元素都比 val 小。 iterator
  • upper_bound(const T & val); 查找一个最小的位置 it,使得[it,end()) 中所有的元素都比 val 大。
  • pair<iterator,iterator> equal_range(const T & val);同时求得lower_bound和upper_bound。
  • iterator erase(iterator it);删除it指向的元素,返回其后面的元素的迭代器(Visual studio 2010上如此,但是在C++标准和Dev C++中,返回值不是这样)。

multiset的用法

#include <iostream>
#include <set> //使用multiset须包含此文件
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
pp = m1.erase(m1.lower_bound(22),m1.upper_bound(22));
//pp指向被删元素的下一个元素
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;
}
//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
pp = m1.erase(m1.lower_bound(22),m1.upper_bound(22));
//pp指向被删元素的下一个元素
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容器

set内不能存在相同的元素(a不小于b且a不大于b成立即相同),要是插入相同的元素,则会忽略插入
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; //输出: 5 inserted
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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值