C++ Array 库 - operator!=() 函数
描述
C++ 函数 bool operator!=() 按顺序比较两个数组容器元素。它使用相等的算法进行比较。 比较在第一次不匹配或容器元素耗尽时停止。 对于两个容器的比较大小和数据类型必须相同,否则编译器将报告编译错误。
声明
以下是 std::array 标头中 bool operator!=() 函数的声明。
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 <array>
#include <iostream>
using namespace std;
int main(void) {
array<int, 5> arr1 = {1, 2, 3, 4, 5};
array<int, 5> arr2 = {1, 2, 3, 4, 5};
array<int, 5> arr3 = {1, 2, 4, 5, 3};
bool result = false;
result = (arr1 != arr2);
if (result == false)
cout << "arr1 and arr2 are equal\n";
else
cout << "arr1 and arr2 are not equal\n";
result = (arr2 != arr3);
if (result == true)
cout << "arr2 and arr3 are not equal\n";
else
cout << "arr2 and arr3 are equal\n";
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
arr1 and arr2 are equal arr2 and arr3 are not equal

