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


C++ upper_bound用法及代碼示例


upper_bound()是在頭文件中定義的C++標準庫函數。它返回一個迭代器,該迭代器指向範圍 [first, last] 中大於 value 的第一個元素,如果沒有找到這樣的元素,則返回最後一個元素。範圍內的元素應已排序或至少已根據 val 進行分區。

模板:

Syntax 1: ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val); 

Syntax 2: ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); 

first, last: The range used is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. 
val: Value of the upper bound to search for in the range. 
comp: Binary function that accepts two arguments (the first of the type pointed by ForwardIterator, and the second, always val), and returns a value convertible to bool. The function shall not modify any of its arguments. This can either be a function pointer or a function object. 

Return type : An iterator to the upper bound of val in the range. If all the element in the range compare less than val, the function returns last.

例子:

Input : 10 20 30 30 40 50
Output : upper_bound for element 30 is at index 4

Input : 10 20 30 40 50
Output : upper_bound for element 45 is at index 4

Input : 10 20 30 40 50
Output : upper_bound for element 60 is at index 5

下麵是一些C++程序來說明std::upper_bound的使用:

CPP


// CPP program to illustrate using 
// std :: upper_bound with vectors
#include <bits/stdc++.h>
// Driver code
int main()
{
    std::vector<int> v{ 10, 20, 30, 40, 50 };
    // Print vector
    std::cout << "Vector contains :";
    for (int i = 0; i < v.size(); i++)
        std::cout << " " << v[i];
    std::cout << "\n";
    std::vector<int>::iterator upper1, upper2;
    // std :: upper_bound
    upper1 = std::upper_bound(v.begin(), v.end(), 35);
    upper2 = std::upper_bound(v.begin(), v.end(), 45);
    std::cout << "\nupper_bound for element 35 is at position : " 
              << (upper1 - v.begin());
    std::cout << "\nupper_bound for element 45 is at position : "
              << (upper2 - v.begin());
    return 0;
}

輸出:

Vector contains : 10 20 30 40 50
upper_bound for element 35 is at position : 3
upper_bound for element 45 is at position : 4

CPP


// CPP program to illustrate using 
// std :: upper_bound with arrays
#include <bits/stdc++.h>
using namespace std;
// Main Function
int main()
{
    int arr[] = { 10, 20, 30, 40, 50 };
    // Print elements of array
    cout << "Array contains :";
    for (int i = 0; i < 5; i++)
        cout << " " << arr[i];
    cout << "\n";
    // using upper_bound
    int upper1 = upper_bound(arr, arr+5, 35) - arr;
    int upper2 = upper_bound(arr, arr+5, 45) - arr;
    cout << "\nupper_bound for element 35 is at position : " 
              << (upper1);
    cout << "\nupper_bound for element 45 is at position : "
              << (upper2);
    return 0;
}

輸出:

Array contains : 10 20 30 40 50
upper_bound for element 35 is at position : 3
upper_bound for element 45 is at position : 4

時間複雜度:執行的比較次數是第一個和最後一個之間的距離的對數。即(最多 log2(last - first) + O(1) 比較)。

Important Points

  • std::upper_bound() 返回一個迭代器,指向傳遞給它的值的上限。
  • std::upper_bound() 僅適用於排序序列。即具有排序元素的向量或具有排序元素的數組。


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 upper_bound in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。