當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。