std::upper_bound
| ヘッダー <algorithm> で定義 |
||
| (1) | ||
template< class ForwardIt, class T > ForwardIt upper_bound( ForwardIt first, ForwardIt last, |
(C++20 以降 constexpr) (C++26まで) |
|
| template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type > |
(C++26以降) | |
| (2) | ||
template< class ForwardIt, class T, class Compare > ForwardIt upper_bound( ForwardIt first, ForwardIt last, |
(C++20 以降 constexpr) (C++26まで) |
|
| template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type, |
(C++26以降) | |
分割された範囲 [first, last) で、value より後に順序付けられる最初の要素を検索します。
|
|
(C++20まで) |
|
std::upper_bound(first, last, value, std::less{}) と同等です。 |
(C++20以降) |
[first, last) 内で、bool(comp(value, *iter)) が true となる最初のイテレータ iter を返します。そのような iter が存在しない場合は last を返します。目次 |
[edit] パラメータ
| first, last | - | 調査する要素の分割された範囲を定義するイテレータのペア |
| value | - | 要素と比較する値 |
| comp | - | 第一引数が第二引数より前に順序付けられる場合に `true` を返す二項述語。 述語関数のシグネチャは、以下と同等である必要がある。 bool pred(const Type1 &a, const Type2 &b); シグネチャは const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関わらず、(おそらく const の) |
| 型要件 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
| -`Compare` は BinaryPredicate の要件を満たす必要があります。 Compare を満たす必要はありません。 | ||
[edit] 戻り値
[first, last) の範囲内で、value より後に順序付けられる最初の要素へのイテレータ。そのような要素が見つからない場合は last。
[edit] 計算量
std::distance(first, last) を N とする
ただし、`ForwardIt` が LegacyRandomAccessIterator でない場合、イテレータのインクリメント回数は N に対して線形になります。特に、std::map、std::multimap、std::set、および std::multiset のイテレータはランダムアクセスではないため、これらのメンバ関数 upper_bound を優先すべきです。
[edit] 実装例
libstdc++ および libc++ の実装を参照してください。
| upper_bound (1) |
|---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T& value) { return std::upper_bound(first, last, value, std::less{}); } |
| upper_bound (2) |
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type, class Compare> ForwardIt upper_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp) { ForwardIt it; typename std::iterator_traits<ForwardIt>::difference_type count, step; count = std::distance(first, last); while (count > 0) { it = first; step = count / 2; std::advance(it, step); if (!comp(value, *it)) { first = ++it; count -= step + 1; } else count = step; } return first; } |
[edit] 注記
std::upper_bound は [first, last) が分割されていることのみを要求しますが、このアルゴリズムは通常、[first, last) がソートされている場合に、任意の value に対して二分探索が有効になるように使用されます。
[first, last) 内の任意のイテレータ iter に対して、std::upper_bound は value < *iter および comp(value, *iter) が有効であることを要求しますが、std::lower_bound は代わりに *iter < value および comp(*iter, value) が有効であることを要求します。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | アルゴリズム (1,2) のためのリスト初期化 |
[edit] 例
#include <algorithm> #include <cassert> #include <complex> #include <iostream> #include <vector> struct PriceInfo { double price; }; int main() { const std::vector<int> data{1, 2, 4, 5, 5, 6}; for (int i = 0; i < 7; ++i) { // Search first element that is greater than i auto upper = std::upper_bound(data.begin(), data.end(), i); std::cout << i << " < "; upper != data.end() ? std::cout << *upper << " at index " << std::distance(data.begin(), upper) : std::cout << "not found"; std::cout << '\n'; } std::vector<PriceInfo> prices{{100.0}, {101.5}, {102.5}, {102.5}, {107.3}}; for (double to_find : {102.5, 110.2}) { auto prc_info = std::upper_bound(prices.begin(), prices.end(), to_find, [](double value, const PriceInfo& info) { return value < info.price; }); prc_info != prices.end() ? std::cout << prc_info->price << " at index " << prc_info - prices.begin() : std::cout << to_find << " not found"; std::cout << '\n'; } using CD = std::complex<double>; std::vector<CD> nums{{1, 0}, {2, 2}, {2, 1}, {3, 0}, {3, 1}}; auto cmpz = [](CD x, CD y) { return x.real() < y.real(); }; #ifdef __cpp_lib_algorithm_default_value_type auto it = std::upper_bound(nums.cbegin(), nums.cend(), {2, 0}, cmpz); #else auto it = std::upper_bound(nums.cbegin(), nums.cend(), CD{2, 0}, cmpz); #endif assert((*it == CD{3, 0})); }
出力
0 < 1 at index 0 1 < 2 at index 1 2 < 4 at index 2 3 < 4 at index 2 4 < 5 at index 3 5 < 6 at index 5 6 < not found 107.3 at index 4 110.2 not found
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 270 | C++98 | Compare は Compare を満たす必要があり、`T` はLessThanComparable (厳密弱順序が必要) である必要がありました。 |
分割のみが必要。 異種比較が許可される。 |
| LWG 384 | C++98 | log2(N)+1 回以下の比較が許可されていました。 | log2(N)+O(1) に修正されました。 |
| LWG 577 | C++98 | last が返される可能性がありました。 | 許可 |
| LWG 2150 | C++98 | もし [first, last) 内に、bool(comp(value, *iter)) が true となるイテレータ iter が存在する場合、 std::upper_bound は[iter, last) 内の任意のイテレータを返す可能性がありました。 |
iter より後のイテレータは 返されません。 |
[edit] 関連項目
| 特定のキーに一致する要素の範囲を返す (関数テンプレート) | |
| 与えられた値より小さくない最初の要素へのイテレータを返す (関数テンプレート) | |
| 要素の範囲を2つのグループに分割する (関数テンプレート) | |
| (C++11) |
パーティション化された範囲のパーティションポイントを見つける (関数テンプレート) |
| (C++20) |
特定の値より大きい最初の要素へのイテレータを返す (アルゴリズム関数オブジェクト) |
| 指定されたキーより大きい最初の要素へのイテレータを返す ( std::set<Key,Compare,Allocator> の public メンバ関数) | |
| 指定されたキーより大きい最初の要素へのイテレータを返す ( std::multiset<Key,Compare,Allocator> の public メンバ関数) |