Cpp-standard-library-cpp-algorithm-is-sorted-v1
提供:Dev Guides
C ++アルゴリズムライブラリ-is_sorted()関数
説明
- C ++関数* std
- algorithm :: is_sorted()*は、範囲がソートされているかどうかをテストします。 比較のために `+ operator <+`を使用します。
宣言
- 以下は、std
- algorithmヘッダーからのstd :: algorithm :: is_sorted()関数の宣言です。
C 11
template <class ForwardIterator>
bool is_sorted (ForwardIterator first, ForwardIterator last);
パラメーター
- first -イテレータを初期位置に転送します。
- last -イテレータを最終位置に転送します。
戻り値
範囲がソートされている場合はtrueを返し、そうでない場合はfalseを返します。
例外
要素比較またはイテレータの操作が例外をスローした場合、例外をスローします。
無効なパラメータは未定義の動作を引き起こすことに注意してください。
時間の複雑さ
リニア。
例
- 次の例は、std
- algorithm :: is_sorted()関数の使用法を示しています。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
vector<int> v = {1, 2, 3, 4, 5};
bool result;
result = is_sorted(v.begin(), v.end());
if (result == true)
cout << "Vector elements are sorted in ascending order." << endl;
v[0] = 10;
result = is_sorted(v.begin(), v.end());
if (result == false)
cout << "Vector elements are not sorted in ascending order." << endl;
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Vector elements are sorted in ascending order.
Vector elements are not sorted in ascending order.