C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset equal_range()

C++ multiset equal_range()

C++ multiset 合 equal_range()函数用于返回包含容器中所有元素的范围边界等于val。
如果val与容器中的任何值都不匹配,则返回值范围将为长度0,并且两个迭代器均指向大于val的最近值。否则,如果val大于容器中的所有元素,则指向结束。

语法

pair<const_iterator,const_iterator> equal_range (const value_type& val) const;
pair<iterator,iterator>                    equal_range (const value_type& val);
范围由两个迭代器定义,一个指向不小于值val的第一个元素,另一个指向大于值val的第一个元素。

参数

val : 要在 multiset 容器中搜索的值。

返回值

此函数返回对。其中pair :: first位于范围的下边界,具有与lower_bound(val)将返回的值相同的值,pair :: second是与upper_bound(val)将返回的值相同,它对应的范围的上限。

复杂度

大小为对数。

迭代器有效性

没有变化。

数据争用

访问容器(无论是const还是非const版本都不能修改容器)。
同时访问 multiset 的元素是安全的。

异常安全性

如果引发异常,则在

示例1

让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   multiset<char> m = {'a','b','c','a'};
   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
    multiset<int> mp;
 
    // insert elements in random order
    mp.insert( 4 );
    mp.insert( 1 );
    mp.insert( 4 );
 
    pair<multiset<int>::const_iterator,multiset<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,因为它试图查找10在 multiset mp中不存在的值,因此,它返回到结尾。

示例3

让我们看一个简单的示例:
#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   typedef multiset<int, less< int > > IntMultiset;  
   IntMultiset s1;  
   multiset <int, less< int > > :: const_iterator s1_RcIter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
  
   pair <IntMultiset::const_iterator, IntMultiset::const_iterator> p1, p2;  
   p1 = s1.equal_range( 20 );  
  
   cout << "The upper bound of the element with "  
        << "a key of 20 in the multiset s1 is: "  
        << *(p1.second) << "." << endl;  
  
   cout << "The lower bound of the element with "  
        << "a key of 20 in the multiset 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 multiset s1 doesn't have an element "  
           << "with a key less than 40." << endl;  
   else  
      cout << "The element of multiset s1 with a key >= 40 is: "  
           << *(p1.first) << "." << endl; 
           
           return 0;
}  
输出:
The upper bound of the element with a key of 20 in the multiset s1 is: 30.
The lower bound of the element with a key of 20 in the multiset 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 multiset s1 doesn't have an element with a key less than 40.

示例4

让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  std::multiset<int> mymultiset;
  for (int i=1; i<=5; i++) mymultiset.insert(i*10);   // mymultiset: 10 20 30 40 50
  pair<std::multiset<int>::const_iterator,multiset<int>::const_iterator> ret;
  ret = mymultiset.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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4