📅  最后修改于: 2020-10-20 01:19:32             🧑  作者: Mango
C++ Multiset upper_bound()函数用于返回一个迭代器,该迭代器指向multiset容器中的值,该值大于参数中传递的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:要在Multiset容器中搜索的值。
upper_bound()函数返回一个迭代器,该迭代器指向Multiset容器中的值,该值大于参数中传递的val。如果没有这样的元素,则返回end()。
大小为对数。
没有变化。
可以访问容器(const版本和非const版本都不能修改Multiset容器)。
同时访问容器的元素是安全的。
如果引发异常,则Multiset合中没有任何更改。
让我们看一个简单的示例,以获取给定值的上限:
#include
#include
using namespace std;
int main(void) {
multiset m = {'a', 'b', 'c', 'b'};
auto it = m.upper_bound('b');
cout << "Upper bound of b is(>): " << *it << endl;
return 0;
}
输出:
Upper bound of b is(>): c
在上面的示例中,当我们尝试找到元素b的上限时,它将返回元素b的较大值,即c
让我们看一个简单的示例,从下限到上限删除Multiset元素:
#include
#include
using namespace std;
int main ()
{
multiset mymultiset;
multiset::iterator itlow,itup;
for (int i=1; i<10; i++) mymultiset.insert(i*10); // 10 20 30 40 50 60 70 80 90
itlow=mymultiset.lower_bound (30); // ^
itup=mymultiset.upper_bound (60); // ^
mymultiset.erase(itlow,itup); // 10 20 70 80 90
cout << "mymultiset contains:";
for (multiset::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
输出:
mymultiset contains: 10 20 70 80 90
在上面的示例中,delete()函数将multiset的元素从下限(=)擦除到上限(>),并print其余内容。
让我们看一个简单的例子:
#include
#include
using namespace std;
int main()
{
// initialize container
multiset 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
在上面的示例中,当我们尝试查找Multiset容器中不存在但未超过最大值的值的上限时,它将返回更大的值
也就是说,当我们尝试找到13的上限时,它将返回14,而当我们试图找到在Multiset中不存在且超过容器最大值的值的上限时,它将返回到end()。
让我们看一个简单的例子:
#include
#include
int main( )
{
using namespace std;
multiset s1;
multiset :: 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 multiset 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 multiset s1 doesn't have an element "
<< "with a key greater than 30." << endl;
else
cout << "The element of multiset s1 with a key > 40 is: "
<< *s1_RcIter << "." << endl;
// The element at a specific location in the multiset 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 multiset s1 with a key greater than 20 is: 30.
The multiset 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.