1、简介
multiset跟set具有相同功能,但允许重复的元素。multiset容器的内部结构通常由平衡二叉树(balancedbinarytree)来实现。当元素放入容器中时,会按照一定的排序法则自动排序,默认是按照less<>排序规则来排序。这种自动排序的特性加速了元素查找的过程,但是也带来了一个问题:不可以直接修改set或multiset容器中的元素值,因为这样做就可能违反了元素自动排序的规则。如果你希望修改一个元素的值,必须先删除原有的元素,再插入新的元素。
2、成员函数
2.1 构造函数
multiset( );
explicit multiset (
const Compare& _Comp
);
multiset (
const Compare& _Comp,
const Allocator& _Al
);
set(
const multiset & _Right
);
template
multiset (
InputIterator _First,
InputIterator _Last
);
template
multiset (
InputIterator _First,
InputIterator _Last,
constCompare& _Comp
);
template
multiset (
InputIterator _First,
InputIterator _Last,
constCompare& _Comp,
constAllocator& _Al
);
参数:
_Al
The storage allocator class to be used for thisset object, which defaults to Allocator.
_Comp
The comparison function of type constTraits usedto order the elements in the set, which defaults to Compare.
_Right
The set of which the constructed set is to be acopy.
_First
The position of the first element in the range ofelements to be copied.
_Last
The position of the first element beyond therange of elements to be copied.
2.2 迭代函数
const_iterator begin( )const;
iterator begin( );
功能:
返回指向multiset头部的迭代器
const_iterator end( )const;
iterator end( );
功能:
返回指向multiset尾部的迭代器
const_reverse_iteratorrbegin( ) const;
reverse_iterator rbegin();
功能:
返回一个指向multiset尾部的逆向迭代器
const_reverse_iterator rend() const;
reverse_iterator rend( );
功能:
返回一个指向multiset首尾部的逆向迭代器
2.3.2擦除数据
iterator erase(
iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);
参数:
_Where
Position of the element to beremoved from the set.
_First
Position of the first elementremoved from the set.
_Last
Position just beyond the lastelement removed from the set.
_Key
The key of the elements to beremoved from the set.
2.3.3交换数据
void swap(
set& _Right
);
参数:
_Right
The argument set providing the elements to beswapped with the target set.
2.3.4清空数据
void clear( );
2.3 操作函数
const_iteratorlower_bound(
const Key& _Key
) const;
iterator lower_bound(
const Key& _Key
);
功能:
返回容器中第一个值大于或等于_Key的元素的iterator位置。
参数:
constKey& _Key, The argument key to becompared with the sort key of an element from the multiset beingsearched.
const_iteratorupper_bound(
const Key& _Key
) const;
iterator upper_bound(
const Key& _Key
);
功能:
返回容器中第一个值大于_Key的元素的iterator位置。
参数:
constKey& _Key, The argument key to becompared with the sort key of an element from the multiset beingsearched.
pair
equal_range (
const Key&_Key
) const;
pair
equal_range (
const Key&_Key
);
功能:
返回容器中值等于val的所有元素的范围[beg,end)组成的pair 。即A pair of iterators such that the first is thelower_bound of the key and the second is the upper_bound of thekey.
参数:
_Key
The argument key to becompared with the sort key of an element from the multiset beingsearched.
3、代码范例
#include
#include
using namespace std;
void PRINT_ELEMENTS(multisetcol1, const char* cstr)
{
multiset::const_iterator pos;
cout << cstr<< endl;
for (pos = col1.begin(); pos != col1.end();++pos)
{
cout<< *pos<< " ";
}
}
int main()
{
multiset col1;
col1.insert(2);
col1.insert(5);
col1.insert(4);
col1.insert(6);
col1.insert(1);
col1.insert(5);
PRINT_ELEMENTS(col1, "col1: ");
cout << endl;
multiset::const_iterator pos;
pair::iterator,multiset::iterator> range;
cout <<"lower_bound(3): " <<*col1.lower_bound(3) << endl;
cout <<"upper_bound(3): " <<*col1.upper_bound(3) << endl;
range = col1.equal_range(3);
cout <<"equal_range(3): " << *range.first<< " "<< *range.second<< endl;
cout << "elementswith value(3): ";
for (pos = range.first; pos != range.second;++pos)
{
cout<< *pos<< " ";
}
cout << endl;
cout << endl;
cout <<"lower_bound(5): " <<*col1.lower_bound(5) << endl;
cout <<"upper_bound(5): " <<*col1.upper_bound(5) << endl;
range = col1.equal_range(5);
cout <<"equal_range(5): " << *range.first<< " "<< *range.second<< endl;
cout << "elementswith value(5): ";
for (pos = range.first; pos != range.second;++pos)
{
cout<< *pos<< " ";
}
cout << endl;
}
输出:
col1:
1 2 4 5 5 6
lower_bound(3): 4
upper_bound(3): 4
equal_range(3): 4 4
elements with value(3):
lower_bound(5): 5
upper_bound(5): 6
equal_range(5): 5 6
elements with value(5): 55