Cpp-standard-library-cpp-algorithm-lower-bound-v2

提供:Dev Guides
移動先:案内検索

C ++アルゴリズムライブラリ-lower_bound()関数

説明

C ++関数* std
algorithm :: lower_bound()*は、指定された値以上の最初の要素を見つけます。 この関数は、ソートされた順序で要素を除きます。 比較のために `+ binary function +`を使用します。

宣言

以下は、std
algorithmヘッダーからのstd :: algorithm :: lower_bound()関数の宣言です。

C 98

template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last,
   const T& val, Compare comp);

パラメーター

  • first -イテレータを初期位置に転送します。
  • last -イテレータを最終位置に転送します。
  • val -範囲内で検索する下限の値。
  • comp -2つの引数を受け取り、boolを返すバイナリ関数。

戻り値

指定された値以上の最初の要素の反復子を返します。 範囲内のすべての要素が `+ val `より小さい場合、関数は ` last +`を返します。

例外

`+ binary function +`またはイテレータの操作が例外をスローした場合、例外をスローします。

無効なパラメータは未定義の動作を引き起こすことに注意してください。

時間の複雑さ

リニア。

次の例は、std
algorithm :: lower_bound()関数の使用法を示しています。
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool ignore_case(char a, char b) {
   return(tolower(a) == tolower(b));
}

int main(void) {
   vector<char> v = {'A', 'b', 'C', 'd', 'E'};
   auto it = lower_bound(v.begin(), v.end(), 'C');

   cout << "First element which is greater than \'C\' is " << *it << endl;

   it = lower_bound(v.begin(), v.end(), 'C', ignore_case);

   cout << "First element which is greater than \'C\' is " << *it << endl;

   it = lower_bound(v.begin(), v.end(), 'z', ignore_case);

   cout << "All elements are less than \'z\'." << endl;

   return 0;
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

First element which is greater than 'C' is b
First element which is greater than 'C' is d
All elements are less than 'z'.