Chapter 16.关联容器multiset

multiset简介
multiset与set类似,但是其允许相同的值,应用举例:比如求某个范围内的最大最小值(值可以重复)
与set的区别在于,insert()一定会插入值;erase(element_value)会删除所有相同元素值的元素
multiset模板
template < class Key, class Compare = less<Key>,
           class Allocator = allocator<Key> > class multiset;

Member functions

(constructor)Construct multiset 
(destructor)Multiset destructor
operator=Copy container content

constructor
1.explicit multiset ( const Compare& comp = Compare(),
                    const Allocator& = Allocator() );//empty version
2.template <class InputIterator>
multiset ( InputIterator first, InputIterator last,
             const Compare& comp = Compare(), const Allocator& = Allocator() );//iterator version
3.multiset ( const multiset<Key,Compare,Allocator>& x );//copy version

eg:
bool fncomp (int lhs, int rhs) {return lhs<rhs;}
struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};
multiset<int,classcomp> ms;                // class as Compare
bool(*fn_pt)(int,int) = fncomp;
multiset<int,bool(*)(int,int)> ms(fn_pt); // function pointer as Compare
Iterators:

beginReturn iterator to beginning
endReturn iterator to end
rbeginReturn reverse iterator to reverse beginning
rendReturn reverse iterator to reverse end

eg:
for (auto iter = ms.rbegin(); iter != ms.rend(); ++iter)
{
	cout<<*iter<<'\t';
}
//same as follow one
#include <algorithm>
#include <iterator>
copy(ms.begin(),ms.end(),ostream_iterator<string>(cout,"\t")); 
Capacity:
emptyTest whether container is empty
sizeReturn container size
max_sizeReturn maximum size
Modifiers:
insertInsert element 
eraseErase elements
swapSwap content
clearClear content
insert 
1.iterator insert ( const value_type& x );//if there are same value elements, all elements removed in this version
2.iterator insert ( iterator position, const value_type& x );
3.template <class InputIterator>
void insert ( InputIterator first, InputIterator last );

eg:
string str[]={"hello","C++","Java","C"};
vector<string> vStr(str,str+4);
multiset<string> ms;

//insert key_value version
ms.insert("1");
ms.insert("1");
ms.insert("3");
ms.insert("1");
auto iter=vStr.begin();
advance(iter,2);

//insert iterator version
ms.insert(vStr.begin(),iter);

//insert (iterator position, key_value)
ms.insert(ms.begin(),"oops");

for (auto iter = ms.begin(); iter != ms.end(); ++iter)
{
	cout<<*iter<<'\t';
}
cout<<endl;
//copy(ms.begin(),ms.end(),ostream_iterator<string>(cout,"\t")); 
Output:
1       1       1       3       C++     hello   oops
erase
1.void erase ( iterator position );
2.size_type erase ( const key_type& x );
3.void erase ( iterator first, iterator last );

eg:
auto cnt=ms.erase("1");
cout<<"-------cnt="<<cnt<<endl;
Output:
-------cnt=3
3       C++     hello   oops
其他两种分别是 iterator position 和 iterator range的版本就不写例子了
Observers:
key_compReturn comparison object
value_compReturn comparison object
//这两个不怎么用就不写了
Operations:
findGet iterator to element
countCount elements with a specific key//return element appear times
lower_boundReturn iterator to lower bound
upper_boundReturn iterator to upper bound
equal_rangeGet range of equal elements
find

//if have no x in container,return multiset::end, else return first one element iterator in container, if have same element you can use equal_range to return a iterator range

iterator find ( const key_type& x ) const;

count
size_type count ( const key_type& x ) const;
equal_range
eg:
//pair<multiset<int>::iterator,multiset<int>::iterator> ret; 
auto ret=ms.equal_range("1"); 
for (auto iter = ret.first; iter != ret.second; ++iter) 
{ 
 cout<<"**********"<<*iter<<'\t'; 
}
//lower_bound >=[包括他在内的他的iterator]
//upper_bound >[比他大的下一个iterator]

eg:
auto low=ms.lower_bound("0"); 
auto up=ms.upper_bound("2");
cout<<*low<<endl;//返回“1”
cout<<*up<<endl; //upper_bound指向了大于“2”的“3”
ms.erase(low,up);
Output://1 1 1 被删除了
3
3 C++ hello oops

Allocator:
get_allocator

//same as before like list etc. omit it
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值