C++ Vector 库 - operator!= 函数
描述
C++ 函数 std::vector::operator!= 测试两个向量是否相等。
运算符 != 首先检查两个容器的大小,如果大小相同,则按顺序比较元素,并在第一次不匹配时停止比较。
声明
以下是 std::vector::operator!= 函数形式 std::vector 头的声明。
template <class T, class Alloc> bool operator!= (const vector<T,Alloc>& v1, const vector<T,Alloc>& v2);
参数
v1 − 第一个向量。
v2 − 第二个向量。
返回值
如果两个向量不相等则返回真,否则返回假。
异常
此函数从不抛出异常。
时间复杂度
线性,即 O(n)
示例
以下示例显示了 std::vector::operator!= 函数的用法。
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
vector<int> v1;
vector<int> v2;
v1.resize(10, 100);
if (v1 != v2)
cout << "v1 and v2 are not equal" << endl;
v1 = v2;
if (!(v1 != v2))
cout << "v1 and v2 are equal" << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
v1 and v2 are not equal v1 and v2 are equal

