当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C++ equal_range用法及代码示例

std::equal_range 用于查找给定范围 [first, last) 内具有等于给定值的所有元素的 sub-range。它返回这样的sub-range的初始和最终边界。

此函数要求根据某种条件对范围进行排序或分区,以便条件评估为 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 <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        vector<int> 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<std::vector<int>::iterator, 
                  std::vector<int>::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 <iostream> 
    #include <algorithm> 
    #include <string> 
    #include <vector> 
    #include <functional> 
    using namespace std; 
      
    // Defining the BinaryFunction 
    bool comp(int a, int b) 
    { 
        return (a > b); 
    } 
    int main() 
    { 
        vector<int> 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<std::vector<int>::iterator, 
                  std::vector<int>::iterator> ip; 
      
        // Sorting the vector v in descending order 
        sort(v.begin(), v.end(), greater<int>()); 
        // 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
    

Where can it be used ?

  1. std::lower_boundstd::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 <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        vector<int> v = { 1, 2, 3, 4, 5, 5, 6, 7 }; 
      
        // Declaring an iterator to store the 
        // return value of std::equal_range 
        std::pair<std::vector<int>::iterator, 
                  std::vector<int>::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<int>::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
    


相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 equal_range in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。