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