C++ set equal_range()函数
C++ set equal_range() 函数用于返回容器中所有与val相等的元素所在范围的边界。由于set容器中没有重复的值,所以这个范围最多包含一个元素。
如果val在容器中找不到与之匹配的值,返回范围的长度将为0,并且两个迭代器都指向大于val的最近的值。否则,如果val大于容器中的所有元素,则指向end。
语法
pair<const_iterator,const_iterator> equal_range (const value_type& val) const;
pair<iterator,iterator> equal_range (const value_type& val);
该范围由两个迭代器定义,一个指向第一个不小于val值的元素,另一个指向第一个大于val值的元素。
参数
val : 在集合容器中要搜索的值。
返回值
该函数返回一个pair。其中pair::first是与lower_bound(val)返回的相同值的范围的下界,pair::second是与upper_bound(val)返回的相同值的范围的上界。
复杂度
大小的对数级别。
迭代器有效性
没有更改。
数据竞争
容器被访问(const和非const版本都不会修改容器)。
同时访问set的元素是安全的。
异常安全性
如果抛出异常,容器不会有任何更改。
示例1
让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<char> m = {'a','b','c','d'};
auto ret = m.equal_range('b');
cout << "Lower bound of b is: " << *ret.first<< endl;
cout << "Upper bound of b is: " << *ret.second<< endl;
return 0;
}
输出结果:
Lower bound of b is: b
Upper bound of b is: c
在上面的示例中,b的下限是b,b的上限是c。
示例2
让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main()
{
// initialize container
set<int> mp;
// insert elements in random order
mp.insert( 4 );
mp.insert( 1 );
mp.insert( 6 );
pair<set<int>::const_iterator,set<int>::const_iterator> ret;
ret = mp.equal_range(10);
cout << "The lower bound is: " << *ret.first;
cout << "\nThe upper bound is: " << *ret.second;
return 0;
}
输出:
The lower bound is 3
The upper bound is 3
在上面的示例中,equal_range()函数返回end()即3,因为它试图查找集合mp中不存在的10。
示例3
让我们看一个简单的示例:
#include <set>
#include <iostream>
int main( )
{
using namespace std;
typedef set<int, less< int > > IntSet;
IntSet s1;
set <int, less< int > > :: const_iterator s1_RcIter;
s1.insert( 10 );
s1.insert( 20 );
s1.insert( 30 );
pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;
p1 = s1.equal_range( 20 );
cout << "The upper bound of the element with "
<< "a key of 20 in the set s1 is: "
<< *(p1.second) << "." << endl;
cout << "The lower bound of the element with "
<< "a key of 20 in the set s1 is: "
<< *(p1.first) << "." << endl;
// Compare the upper_bound called directly
s1_RcIter = s1.upper_bound( 20 );
cout << "A direct call of upper_bound( 20 ) gives "
<< *s1_RcIter << "," << endl
<< "matching the 2nd element of the pair"
<< " returned by equal_range( 20 )." << endl;
p2 = s1.equal_range( 40 );
// If no match is found for the key,
// both elements of the pair return end( )
if ( ( p2.first == s1.end( ) ) && ( p2.second == s1.end( ) ) )
cout << "The set s1 doesn't have an element "
<< "with a key less than 40." << endl;
else
cout << "The element of set s1 with a key >= 40 is: "
<< *(p1.first) << "." << endl;
return 0;
}
输出结果:
The upper bound of the element with a key of 20 in the set s1 is: 30.
The lower bound of the element with a key of 20 in the set s1 is: 20.
A direct call of upper_bound( 20 ) gives 30,
matching the 2nd element of the pair returned by equal_range( 20 ).
The set s1 doesn't have an element with a key less than 40.
示例4
让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
std::set<int> myset;
for (int i=1; i<=5; i++) myset.insert(i*10); // myset: 10 20 30 40 50
pair<std::set<int>::const_iterator,set<int>::const_iterator> ret;
ret = myset.equal_range(30);
cout << "The lower bound points to: " << *ret.first << '\n';
cout << "The upper bound points to: " << *ret.second << '\n';
return 0;
}
输出:
The lower bound points to: 30
The upper bound points to: 40