📌  相关文章
📜  C++中std :: set :: lower_bound和std :: lower_bound之间的区别

📅  最后修改于: 2021-05-30 10:28:22             🧑  作者: Mango

先决条件: C++中的随机访问迭代器,C++中的双向迭代器。

C++中的std :: lower_bound:
C++中的lower_bound()方法用于返回一个迭代器,该迭代器指向[first,last)范围内的第一个元素,该元素的值不少于给定值。这意味着该函数返回的下一个最小数字的索引正好大于该数字。

C++中的std :: set :: lower_bound
set :: lower_bound()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向容器中的元素,该元素等于参数中传递的K。如果set容器中不存在K,则该函数返回一个迭代器,该迭代器指向刚好大于K的下一个元素。如果在参数中传递的键超过了容器中的最大值,则返回的迭代器将指向设置容器中的最后一个元素。

以下是std :: lower_boundstd :: set :: lower_bound之间的区别

S.No. std::lower_bound() std::set::lower_bound()
1 Syntax: std::lower_bound(ForwardIterator first, ForwardIterator last, const T& val). Syntax: std::lower_bound(const value_type& val).
2 The std::lower_bound has Random Access Iterators and Non Random Access Iterators. The std::set::lower_bound has Bidirectional Iterators.
3 This function optimizes the number of comparisons which is efficient for Random Access Iterators. This function optimises the number of comparisons using Bidirectional Iterators
4 The running time complexity is O(log2N) for random-access iterators but for non random-access iterators it is O(N). The running time complexity is always O(log2N) due to it’s Binary Search Tree implementation.

下面是带有CPU执行时间的程序,它将说明两个功能的运行时间。

用来说明std :: lower_bound()的程序

// C++ program to illustrate
// std::set::lower_bound
#include 
#include 
using namespace std;
  
// Function whose time is to
// be measured
void fun()
{
    // Initialise the set
    set s;
  
    // Insert element in the set
    for (int i = 0; i < 10; i++) {
        s.insert(i);
    }
  
    // Use lower_bound() function
    // to find 5
    set::iterator it;
    it = lower_bound(s.begin(), s.end(), 5);
}
  
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
  
    // Start timer
    gettimeofday(&start, NULL);
  
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
  
    // Function Call
    fun();
  
    // Stop timer
    gettimeofday(&end, NULL);
  
    // Calculating total time taken
    // by the program.
    double time_taken;
  
    time_taken = (end.tv_sec
                  - start.tv_sec)
                 * 1e6;
  
    time_taken = (time_taken
                  + (end.tv_usec
                     - start.tv_usec))
                 * 1e-6;
  
    cout << "Time taken by program is : "
         << fixed
         << time_taken << setprecision(6);
    cout << " sec" << endl;
    return 0;
}
输出:
Time taken by program is : 0.000046 sec

用来说明std :: set :: lower_bound()的程序

// C++ program to illustrate
// std::lower_bound
#include 
#include 
using namespace std;
  
// Function whose time is to
// be measured
void fun()
{
    // Initialise the set
    set s;
  
    // Insert element in the set
    for (int i = 0; i < 10; i++) {
        s.insert(i);
    }
  
    // Use set::lower_bound() function
    // to find 5
    set::iterator it;
    it = s.lower_bound(5);
}
  
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
  
    // Start timer
    gettimeofday(&start, NULL);
  
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
  
    fun();
  
    // Stop timer
    gettimeofday(&end, NULL);
  
    // Calculating total time taken
    // by the program.
    double time_taken;
  
    time_taken = (end.tv_sec
                  - start.tv_sec)
                 * 1e6;
  
    time_taken = (time_taken
                  + (end.tv_usec
                     - start.tv_usec))
                 * 1e-6;
  
    cout << "Time taken by program is : "
         << fixed
         << time_taken << setprecision(6);
    cout << " sec" << endl;
    return 0;
}
输出:
Time taken by program is : 0.000039 sec
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”