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


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

C++ Multiset lower_bound() 函數用於返回一個迭代器,該迭代器指向 multiset 容器中的鍵,相當於傳入參數中的 val。

如果 val 不存在於 multiset 容器中,則它返回一個迭代器,指向剛好大於 val 的緊鄰下一個元素。

用法

iterator lower_bound (const value_type& val) const;                //until C++ 11
iterator lower_bound (const value_type& val);                        //since C++ 11
const_iterator lower_bound (const value_type& val) const;      //since C++ 11

參數

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

返回值

它返回一個迭代器,指向多重集容器中的值,相當於傳入參數的 val。如果沒有這樣的元素返回 end()。

複雜度

大小為對數。

迭代器有效性

沒有變化。

數據競爭

訪問容器(常量和非常量版本都不會修改多集)。

同時訪問容器的元素是安全的。

異常安全

如果拋出異常,則多重集中沒有變化。

例子1

讓我們看一個簡單的例子來獲取給定鍵的下界:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<char> m = {'a','b','c','a','c'};
          
   auto it = m.lower_bound('c');

   cout << "Lower bound(=) of c is:" << *it;
   return 0;
}

輸出:

Lower bound(=) of c is:c

在上麵的例子中,c 的下限是 c。

例子2

讓我們看一個簡單的例子,將 multiset 的元素從下限擦除到上限:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<int> mymultiset;
  multiset<int>::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

  std::cout << "mymultiset contains:";
  for (multiset<int>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

輸出:

mymultiset contains:10 20 70 80 90

在上麵的例子中,erase() 函數將 multiset 的元素從下限(=)到上限(>)擦除並打印剩餘的內容。

例子3

讓我們看一個簡單的例子:

#include <set>  
#include <iostream>  

using namespace std;
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1;  
   multiset <int>::const_iterator s1_AcIter, s1_RcIter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
  
   s1_RcIter = s1.lower_bound( 20 );  
   cout << "The element of multiset s1 with a key of 20 is:"  
        << *s1_RcIter << "." << endl;  
  
   s1_RcIter = s1.lower_bound( 40 );  
  
   // 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 of 40." << endl;  
   else  
      cout << "The element of multiset s1 with a key of 40 is:"  
           << *s1_RcIter << "." << endl;  
  
   // The element at a specific location in the multiset can be found   
   // by using a dereferenced iterator that addresses the location  
   s1_AcIter = s1.end( );  
   s1_AcIter--;  
   s1_RcIter = s1.lower_bound( *s1_AcIter );  
   cout << "The element of s1 with a key matching "  
        << "that of the last element is:"  
        << *s1_RcIter << "." << endl;  
        
        return 0;
}

輸出:

The element of multiset s1 with a key of 20 is:20.
The multiset s1 doesn't have an element with a key of 40.
The element of s1 with a key matching that of the last element is:30.

示例 4

讓我們看一個簡單的例子:

#include<set>
#include<iostream>

using namespace std;
 
int main()
{
 
    multiset<int> mp; 
    // insert elements in random order
    mp.insert( 2 );
    mp.insert( 1 );
    mp.insert( 5 );
    mp.insert( 4 );
    
    cout<<"Elements are:\n";
    for (auto it = mp.begin(); it != mp.end(); it++) {
        cout << (*it)<< endl;
    }
 
    // when 2 is present
    auto it = mp.lower_bound(2);
    cout << "The lower bound of key 2 is ";
    cout << (*it)<< endl;
 
    // when 3 is not present
    // points to next greater after 3
    it = mp.lower_bound(3);
    cout << "The lower bound of key 3 is ";
    cout << (*it)<< endl;
 
    // when 6 exceeds
    it = mp.lower_bound(6);
    cout << "The lower bound of key 6 is ";
    cout << (*it);
    
    return 0;
}

輸出:

Elements are:
1
2
4
5
The lower bound of key 2 is 2
The lower bound of key 3 is 4
The lower bound of key 6 is 4

在上麵的例子中,當我們試圖找到一個超出容器的值的下限時,或者我們可以說多集容器中不存在的值,那麽它將返回到結尾。





相關用法


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