📜  C++中的equal_range

📅  最后修改于: 2021-05-30 18:48:59             🧑  作者: Mango

std :: equal_range用于查找给定范围[first,last)中的子范围,该子范围具有等于给定值的所有元素。它返回该子范围的初始和最终界限。

此函数要求根据某种条件对范围进行排序或分区,以使条件求值为true的所有元素都在给定值的左侧,其余所有元素都在其右侧。

可以按以下两种方式使用它:

  1. 使用<:比较元素

    句法:

    Template
    pair 
        equal_range (ForwardIterator first, ForwardIterator last, const T& val);
    
    first: Forward iterator to the first element in the range.
    last: Forward iterator to the last element in the range.
    val: Value of the subrange to search for in the range.
    
    Return Value: It returns a pair object, whose member pair::first 
    is an iterator to the lower bound of the subrange of equivalent 
    values, and pair::second its upper bound.
    If there is no element equivalent to val, then both first and 
    second points to the nearest element greater than val, or if val is
    greater than any other value, then both of them point to last.
    
    // C++ program to demonstrate the use of std::equal_range
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 10, 10, 30, 30, 30, 100, 10,
                          300, 300, 70, 70, 80 };
      
        // Declaring an iterator to store the
        // return value of std::equal_range
        std::pair::iterator,
                  std::vector::iterator> ip;
      
        // Sorting the vector v
        sort(v.begin(), v.end());
        // v becomes 10 10 10 30 30 30 70 70 80 100 300 300
      
        // Using std::equal_range and comparing the elements
        // with 30
        ip = std::equal_range(v.begin(), v.begin() + 12, 30);
      
        // Displaying the subrange bounds
        cout << "30 is present in the sorted vector from index "
             << (ip.first - v.begin()) << " till "
             << (ip.second - v.begin());
      
        return 0;
    }
    

    输出:

    30 is present in the sorted vector from index 3 till 6
    

    说明:在对向量v1进行排序之后,我们检查了存在30的边界,即从索引3到索引6。

  2. 通过使用预定义函数进行比较:

    句法:

    pair 
        equal_range (ForwardIterator first, ForwardIterator last, 
                     const T& val, Compare comp);
    
    Here, first, last and val are the same as previous case.
    
    comp: Binary function that accepts two arguments of the type 
    pointed by ForwardIterator (and of type T), and returns a
    value convertible to bool. The value returned indicates 
    whether the first argument is considered to go before the
    second. 
    The function shall not modify any of its arguments.
    This can either be a function pointer or a function object.
    
    Return Value: It returns a pair object, whose member 
    pair::first is an iterator to the lower bound of the subrange 
    of equivalent values, and pair::second its upper bound. 
    If there is no element equivalent to val, then both first and
    second point to the nearest element greater than val,
    or if val is greater than any other value, then both
    of them point to last.
    
    // C++ program to demonstrate the use of std::equal_range
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
      
    // Defining the BinaryFunction
    bool comp(int a, int b)
    {
        return (a > b);
    }
    int main()
    {
        vector v = { 10, 10, 30, 30, 30, 100, 10,
                          300, 300, 70, 70, 80 };
      
        // Declaring an iterator to store the
        // return value of std::equal_range
        std::pair::iterator,
                  std::vector::iterator> ip;
      
        // Sorting the vector v in descending order
        sort(v.begin(), v.end(), greater());
        // v becomes 300 300 100 80 70 70 30 30 30 10 10 10
      
        // Using std::equal_range and comparing the elements
        // with 10
        ip = std::equal_range(v.begin(), v.begin() + 12, 10, comp);
      
        // Displaying the subrange bounds
        cout << "10 is present in the sorted vector from index "
             << (ip.first - v.begin()) << " till "
             << (ip.second - v.begin());
      
        return 0;
    }
    

    输出:

    10 is present in the sorted vector from index 9 till 12
    

在哪里可以使用?

  1. std :: lower_bound和std :: upper_bound在一个地方:如果我们要同时使用std :: lower_bound和std :: upper_bound,则可以使用此函数,因为它的第一个指针与std :: lower_bound相同其第二个指针将与std :: upper_bound相同。因此,如果我们有std :: equal_range,则无需单独使用它们。
    // C++ program to demonstrate the use of std::equal_range
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 1, 2, 3, 4, 5, 5, 6, 7 };
      
        // Declaring an iterator to store the
        // return value of std::equal_range
        std::pair::iterator,
                  std::vector::iterator> ip;
      
        // Using std::equal_range and comparing the elements
        // with 5
        ip = std::equal_range(v.begin(), v.end(), 5);
      
        // Displaying the subrange bounds
        cout << "std::lower_bound should be equal to "
             << (ip.first - v.begin()) << " and std::upper_bound "
             << "should be equal to " << (ip.second - v.begin());
      
        vector::iterator i1, i2;
      
        // Using std::lower_bound
        i1 = std::lower_bound(v.begin(), v.end(), 5);
        cout << "\nstd::lower_bound is = " << (i1 - v.begin());
      
        // Using std::upper_bound
        i2 = std::upper_bound(v.begin(), v.end(), 5);
        cout << "\nstd::upper_bound is = " << (i2 - v.begin());
      
        return 0;
    }
    

    输出:

    std::lower_bound should be equal to 4 and 
    std::upper_bound should be equal to 6
    std::lower_bound is = 4
    std::upper_bound is = 6
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”