multiset::equal_range
返回一个对迭代器分别为多个集合的第一个元素与大于指定的键的高一个键和多对集的第一个元素与大于或等于该的。
pair <const_iterator, const_iterator>
equal_range (
const Key& _Key
) const;
pair <iterator, iterator>
equal_range (
const Key& _Key
);
参数
- _Key
参数用键元素的排序关键字进行比较从搜索的多级集的。
返回值
一对迭代器的第一和第二是 lower_bound 键是键的 upper_bound。
若要访问受对 pr 的第一个迭代器由成员函数返回,则使用 pr。first和取消引用更低限制的迭代器,使用* (pr。first)。 若要访问受对 pr 的第二个迭代器由成员函数返回,则使用 pr。秒取消引用和上限的迭代器,使用* (pr。秒)。
示例
// multiset_equal_range.cpp
// compile with: /EHsc
#include <set>
#include <iostream>
int main( )
{
using namespace std;
typedef multiset<int, less<int> > IntSet;
IntSet ms1;
multiset <int> :: const_iterator ms1_RcIter;
ms1.insert( 10 );
ms1.insert( 20 );
ms1.insert( 30 );
pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;
p1 = ms1.equal_range( 20 );
cout << "The upper bound of the element with "
<< "a key of 20 in the multiset ms1 is: "
<< *( p1.second ) << "." << endl;
cout << "The lower bound of the element with "
<< "a key of 20 in the multiset ms1 is: "
<< *( p1.first ) << "." << endl;
// Compare the upper_bound called directly
ms1_RcIter = ms1.upper_bound( 20 );
cout << "A direct call of upper_bound( 20 ) gives "
<< *ms1_RcIter << "," << endl
<< "matching the 2nd element of the pair"
<< " returned by equal_range( 20 )." << endl;
p2 = ms1.equal_range( 40 );
// If no match is found for the key,
// both elements of the pair return end( )
if ( ( p2.first == ms1.end( ) ) && ( p2.second == ms1.end( ) ) )
cout << "The multiset ms1 doesn't have an element "
<< "with a key less than 40." << endl;
else
cout << "The element of multiset ms1 with a key >= 40 is: "
<< *( p1.first ) << "." << endl;
}
要求
标头: <set>
命名空间: std