當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Multiset equal_range()用法及代碼示例

C++ Multiset equal_range()函數用於返回包含容器中所有等於val的元素的範圍的邊界。

如果 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::first 位於與lower_bound (val) 將返回的值相同的範圍的下限,pair::second 與upper_bound (val) 將返回的值相同,它對應的範圍的上限.

複雜度

大小為對數。

迭代器有效性

沒有變化。

數據競爭

訪問容器(const 和 non-const 版本都不會修改容器)。

同時訪問多重集的元素是安全的。

異常安全

如果拋出異常,則容器中沒有變化。

例子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 不存在於多重集 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




相關用法


注:本文由純淨天空篩選整理自 C++ Multiset equal_range()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。