C++ Array 库 - operator<() 函数
描述
C++ 函数 bool operator<() 按顺序比较两个数组容器元素。 比较在第一次不匹配或容器元素耗尽时停止。 对于两个容器的比较大小和数据类型必须相同,否则编译器将报告编译错误。
声明
以下是 bool operator<() 函数形式 std::array 头的声明。
template <class T, size_t N> bool operator< ( const array<T,N>& arr1, const array<T,N>& arr2 );
参数
arr1 和 arr2 − 两个相同大小和类型的数组容器。
返回值
如果第一个数组容器小于第二个,则返回 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, 5, 3, 4};
bool result;
result = (arr1 < arr2);
if (result == true)
cout << "arr1 is less than arr2\n";
else
cout << "arr2 is less that arr1\n";
result = (arr3 < arr2);
if (result == false)
cout << "arr2 is less than arr3\n";
else
cout << "arr3 is less than arr2\n";
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
arr1 is less than arr2 arr2 is less than arr3

