Cpp-standard-library-cpp-array-less-than-or-equal-to
提供:Dev Guides
C ++配列ライブラリ-operator ⇐()関数
説明
C ++関数* bool operator ⇐()*は、2つの配列コンテナー要素を順番に比較します。 比較は、最初の不一致またはコンテナ要素が大量に発生したときに停止します。 比較の場合、両方のコンテナのサイズとデータ型は同じでなければなりません。そうしないと、コンパイラはコンパイルエラーを報告します。
宣言
- 以下は、std
- arrayヘッダーからのbool operator ⇐()関数の宣言です。
template <class T, size_t N>
bool operator<= ( const array<T,N>& arr1, const array<T,N>& arr2 );
パラメーター
*arr1およびarr2* -同じサイズとタイプの2つの配列コンテナ。
戻り値
最初の配列コンテナが2番目のコンテナ以下の場合はtrueを返し、そうでない場合はfalseを返します。
例外
この関数は例外をスローしません。
時間の複雑さ
線形、すなわち O(n)
例
次の例は、bool operator ⇐()関数の使用法を示しています。
#include <iostream>
#include <array>
using namespace std;
int main(void) {
array<int, 5> arr1 = {1, 2, 3, 4, 5};
array<int, 5> arr2 = {1, 2, 4, 3, 5};
array<int, 5> arr3 = {1, 2, 1, 4, 3};
bool result;
result = (arr1 < arr2);
if (result == true)
cout << "arr1 is less than or equal to arr2\n";
else
cout << "arr2 is not less that or equal to arr1\n";
result = (arr1 < arr3);
if (result == false)
cout << "arr1 is not less than or equal to arr3\n";
else
cout << "arr1 is less than or equal to arr3\n";
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
arr1 is less than or equal to arr2
arr1 is not less than or equal to arr3