C++ set upper_bound()函数
C++ set upper_bound() 函数用于返回指向集合容器中较大于参数val的值的迭代器。
语法
iterator upper_bound (const value_type& val) const; //until C++ 11
iterator upper_bound (const value_type& val); //since C++ 11
const_iterator upper_bound (const value_type& val) const; //since C++ 11
参数
val :要在集合容器中搜索的值。
返回值
返回一个指向集合容器中大于参数中的值的迭代器。如果没有这样的元素,则返回end()。
复杂度
对数级。
迭代器的有效性
没有变化。
数据竞争
访问容器(无论是const还是非const版本都不会修改容器)。
同时访问set的元素是安全的。
异常
如果抛出异常,容器中没有任何变化。
示例1
让我们来看一个简单的示例,获取给定值的上界:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<char> m = {'a', 'b', 'c', 'd'};
auto it = m.upper_bound('b');
cout << "Upper bound of b is(>): " << *it << endl;
return 0;
}
输出:
Upper bound of b is(>): c
在上面的示例中,当我们试图找到元素b的上界时,它将返回较大的元素,即c。
示例2
让我们看一个简单的示例,将集合中的元素从下界到上界擦除:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> myset;
set<int>::iterator itlow,itup;
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
itlow=myset.lower_bound (30); // ^
itup=myset.upper_bound (60); // ^
myset.erase(itlow,itup); // 10 20 70 80 90
std::cout << "myset contains:";
for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
输出:
myset contains: 10 20 70 80 90
在上面的示例中,erase()函数从下限(=)到上限(>)擦除了集合的元素,并打印剩余内容。
示例3
让我们看一个简单的示例:
#include<iostream>
#include<set>
using namespace std;
int main()
{
// initialize container
set<int> mp;
// insert elements in random order
mp.insert( 12 );
mp.insert( 11 );
mp.insert( 15 );
mp.insert( 14 );
// when 11 is present
auto it = mp.upper_bound(11);
cout << "The upper bound of key 11 is ";
cout << (*it)<< endl;
// when 13 is not present
it = mp.upper_bound(13);
cout << "The upper bound of key 13 is ";
cout << (*it)<< endl;
// when 17 is exceeds the maximum key, so size
// of mp is returned as key and value as 0.
it = mp.upper_bound(17);
cout << "The upper bound of key 17 is ";
cout << (*it);
return 0;
}
输出:
The upper bound of key 11 is 12
The upper bound of key 13 is 14
The upper bound of key 17 is 4
在上面的示例中,当我们尝试查找不在集合容器中但不超过最大值的值的上界时,它将返回较大的值,即当我们尝试查找13的上界时,它将返回14,当我们尝试查找不在集合中且超过容器最大值的值的上界时,它将返回到end()。
示例4
让我们看一个简单的示例:
#include <set>
#include <iostream>
int main( )
{
using namespace std;
set <int> s1;
set <int> :: const_iterator s1_AcIter, s1_RcIter;
s1.insert( 10 );
s1.insert( 20 );
s1.insert( 30 );
s1_RcIter = s1.upper_bound( 20 );
cout << "The first element of set s1 with a key greater "
<< "than 20 is: " << *s1_RcIter << "." << endl;
s1_RcIter = s1.upper_bound( 30 );
// If no match is found for the key, end( ) is returned
if ( s1_RcIter == s1.end( ) )
cout << "The set s1 doesn't have an element "
<< "with a key greater than 30." << endl;
else
cout << "The element of set s1 with a key > 40 is: "
<< *s1_RcIter << "." << endl;
// The element at a specific location in the set can be found
// by using a dereferenced iterator addressing the location
s1_AcIter = s1.begin( );
s1_RcIter = s1.upper_bound( *s1_AcIter );
cout << "The first element of s1 with a key greater than"
<< endl << "that of the initial element of s1 is: "
<< *s1_RcIter << "." << endl;
return 0;
}
输出:
The first element of set s1 with a key greater than 20 is: 30.
The set s1 doesn't have an element with a key greater than 30.
The first element of s1 with a key greater than
that of the initial element of s1 is: 20.