C++ Set 中的 lower_bound函数

首页 / C++入门教程 / C++ Set 中的 lower_bound函数

C++ set lower_bound()函数用于返回指向set集合中键的迭代器,该迭代器等效于参数中传递的val。

如果set集合中不存在val,它将返回一个迭代器,该迭代器指向刚好大于val的直接下一个元素。

lower_bound - 语法

iterator lower_bound (const value_type& val);                        //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    

lower_bound - 参数

val :要在设置的集合中搜索的值。

lower_bound - 返回值

它返回一个指向设置集合中值的迭代器,该迭代器等效于在参数中传递的val。如果没有这样的元素,则返回end()

lower_bound - 例子1

让我们看一个简单的示例,以获取给定键的下限:

#include <iostream>
#include <set>

using namespace std;

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

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

输出:

Lower bound is(=) c

在上面的示例中,c的下限是c。

lower_bound - 例子2

让我们看一个简单的示例,从下限到上限擦除set的元素:

#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

在上面的示例中,delete()函数将set的元素从下限(=)擦除到上限(>),并打印其余内容。

lower_bound - 例子3

让我们看一个简单的例子:

#include <set>  
#include <iostream>  

using namespace std;
  
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.lower_bound( 20 );  
   cout << "The element of set 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 set s1 doesn't have an element "  
           << "with a key of 40." << endl;  
   else  
      cout << "The element of set s1 with a key of 40 is: "  
           << *s1_RcIter << "." << endl;  
  
  //The element at a specific location in the set 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 set s1 with a key of 20 is: 20.
The set 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.

lower_bound - 例子4

让我们看一个简单的例子:

#include<set>
#include<iostream>

using namespace std;
 
int main()
{
 
    set<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

在上面的示例中,当我们尝试查找超出集合的值的下限时,或者可以说set集合中不存在该值的下限时,它将返回到end。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

硅谷产品实战36讲 -〔曲晓音〕

MySQL实战45讲 -〔林晓斌〕

全栈工程师修炼指南 -〔熊燚(四火)〕

移动端自动化测试实战 -〔思寒〕

设计模式之美 -〔王争〕

说透敏捷 -〔宋宁〕

容器实战高手课 -〔李程远〕

大厂广告产品心法 -〔郭谊〕

深入浅出可观测性 -〔翁一磊〕

好记忆不如烂笔头。留下您的足迹吧 :)