📌  相关文章
📜  C++ STL中的二进制搜索功能(binary_search,lower_bound和upper_bound)

📅  最后修改于: 2021-05-30 05:02:53             🧑  作者: Mango

二进制搜索是竞争性编程或任何算法竞争中的重要组成部分,因为具有速记功能的知识,可以减少对它们进行编码的时间。该搜索仅在对容器进行排序时有效。相关功能在下面讨论。

1.binary_search(start_ptr,end_ptr,num)如果元素存在于容器中,此函数返回boolean true ,否则返回false。

// C++ code to demonstrate the working of binary_search()
  
#include 
using namespace std;
  
int main()
{
    // initializing vector of integers
    vector arr = {10, 15, 20, 25, 30, 35};
      
    // using binary_search to check if 15 exists
    if (binary_search(arr.begin(), arr.end(), 15))
       cout << "15 exists in vector";
    else 
       cout << "15 does not exist";
       
    cout << endl;
      
    // using binary_search to check if 23 exists
    if (binary_search(arr.begin(), arr.end(), 23))
         cout << "23 exists in vector";
    else 
         cout << "23 does not exist";
       
    cout << endl;    
}

输出:

15 exists in vector
23 does not exist

2.lower_bound(start_ptr,end_ptr,num) :如果容器包含1次num,则返回指向“ num的位置”的指针。如果容器包含num多次出现,则返回指向“ num的第一个位置”的指针。如果容器不包含num,则返回指向“下一个比num高的数字的位置”的指针。将指针减去到第一位置即“ vect.begin() ”将返回实际索引。

// C++ code to demonstrate the working of lower_bound()
#include 
using namespace std;
  
int main()
{
    // initializing vector of integers
    // for single occurrence
    vector arr1 = {10, 15, 20, 25, 30, 35};
      
    // initializing vector of integers
    // for multiple occurrences
    vector arr2 = {10, 15, 20, 20, 25, 30, 35};
      
    // initializing vector of integers
    // for no occurrence
    vector arr3 = {10, 15, 25, 30, 35};    
      
    // using lower_bound() to check if 20 exists
    // single occurrence
    // prints 2
    cout << "The position of 20 using lower_bound "
            " (in single occurrence case) : ";
    cout << lower_bound(arr1.begin(), arr1.end(), 20) 
            - arr1.begin();
       
    cout << endl;
      
    // using lower_bound() to check if 20 exists
    // multiple occurrence
    // prints 2
    cout << "The position of 20 using lower_bound "
             "(in multiple occurrence case) : ";
    cout << lower_bound(arr2.begin(), arr2.end(), 20) 
            - arr2.begin();
       
    cout << endl;
       
    // using lower_bound() to check if 20 exists
    // no occurrence
    // prints 2 ( index of next higher)
    cout << "The position of 20 using lower_bound "
             "(in no occurrence case) : ";
    cout << lower_bound(arr3.begin(), arr3.end(), 20) 
            - arr3.begin();
       
    cout << endl;    
}

输出:

The position of 20 using lower_bound (in single occurrence case) : 2
The position of 20 using lower_bound (in multiple occurrence case) : 2
The position of 20 using lower_bound (in no occurrence case) : 2

3. upper_bound(start_ptr,end_ptr,num) :如果容器包含1次num,则返回指向“下一个比num高的数字的位置”的指针。如果容器包含num的多次出现,则返回指向“比上次出现的num高一个下一个数字的第一个位置”的指针。如果容器不包含num,则返回指向“下一个比num高的数字的位置”的指针。将指针减去到第一位置即“ vect.begin() ”将返回实际索引。

// C++ code to demonstrate the working of upper_bound()
#include 
using namespace std;
  
int main()
{
    // initializing vector of integers
    // for single occurrence
    vector arr1 = {10, 15, 20, 25, 30, 35};
      
    // initializing vector of integers
    // for multiple occurrences
    vector arr2 = {10, 15, 20, 20, 25, 30, 35};
      
    // initializing vector of integers
    // for no occurrence
    vector arr3 = {10, 15, 25, 30, 35};
      
      
    // using upper_bound() to check if 20 exists
    // single occurrence
    // prints 3
    cout << "The position of 20 using upper_bound"
           " (in single occurrence case) : ";
    cout << upper_bound(arr1.begin(), arr1.end(), 20)
            - arr1.begin();
       
    cout << endl;
      
    // using upper_bound() to check if 20 exists
    // multiple occurrence
    // prints 4
    cout << "The position of 20 using upper_bound "
             "(in multiple occurrence case) : ";
    cout << upper_bound(arr2.begin(), arr2.end(), 20) 
            - arr2.begin();
       
    cout << endl;
       
    // using upper_bound() to check if 20 exists
    // no occurrence
    // prints 2 ( index of next higher)
    cout << "The position of 20 using upper_bound"
            " (in no occurrence case) : ";
    cout << upper_bound(arr3.begin(), arr3.end(), 20) 
           - arr3.begin();
       
    cout << endl;    
}

输出:

The position of 20 using upper_bound (in single occurrence case) : 3
The position of 20 using upper_bound (in multiple occurrence case) : 4
The position of 20 using upper_bound (in no occurrence case) : 2
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”