Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

relational operators (array)

函数模板  <array>
(1)	template <class T, size_T N>
  bool operator== ( const array<T,N>& lhs, const array<T,N>& rhs );
(2)	template <class T, size_T N>
  bool operator!= ( const array<T,N>& lhs, const array<T,N>& rhs );
(3)	template <class T, size_T N>
  bool operator<  ( const array<T,N>& lhs, const array<T,N>& rhs );
(4)	template <class T, size_T N>
  bool operator<= ( const array<T,N>& lhs, const array<T,N>& rhs );
(5)	template <class T, size_T N>
  bool operator>  ( const array<T,N>& lhs, const array<T,N>& rhs );
(6)	template <class T, size_T N>
  bool operator>= ( const array<T,N>& lhs, const array<T,N>& rhs );

数组的关系运算符

在数组容器lhs和rhs之间执行相应的比较操作。

相等比较(operator==)通过使用operator==顺序比较元素来执行, 在第一个不匹配处停止(就像使用algorithm equal一样)

小于比较(operator<)的行为就像使用运算法则lexicographal_compare, 它使用operator<以对等的方式顺序比较元素(即,检查a<b和b<a),并在第一次出现时停止.

其他操作也在内部使用通过使用operator==和<来比较元素,就像执行了以下等价操作一样:
操作符 等价操作
a!=b !(a==b)
a>b b<a
a<=b !(b<a)
a>=b !(a<b)

这些操作符在头文件<array>中重载。

☲  参数


lhs, rhs
具有相同模板形参(T和N)的数组容器(分别位于操作符的左侧和右侧)。

☣  示例



// array comparisons
#include <iostream>
#include <array>

int main ()
{
  std::array<int,5> a = {10, 20, 30, 40, 50};
  std::array<int,5> b = {10, 20, 30, 40, 50};
  std::array<int,5> c = {50, 40, 30, 20, 10};

  if (a==b) std::cout << "a and b are equal\n";
  if (b!=c) std::cout << "b and c are not equal\n";
  if (b<c) std::cout << "b is less than c\n";
  if (c>b) std::cout << "c is greater than b\n";
  if (a<=b) std::cout << "a is less than or equal to b\n";
  if (a>=b) std::cout << "a is greater than or equal to b\n";

  return 0;
}

输出:
a and b are equal
b and c are not equal
b is less than c
c is greater than b
a is less than or equal to b
a is greater than or equal to b

☉  返回值



如果条件成立,则为真,否则为假。

✥ 复杂度



可达到线性(N)

☣ 迭代器的有效性



不变

⇄ 数据竞争


最多可以访问lhs和rhs中所包含的所有元素。

☂ 异常安全性



如果元素的类型支持有no-throw保证的适当操作,则函数永远不会抛出异常(no-throw保证)。
在任何情况下,函数都不能修改它的参数。

🍄  另请参阅



array::size 返回大小(公众成员函数)
array::swap 交换元素(公众成员函数)

联系我们 免责声明 关于CandCplus 网站地图