C++官网参考链接:https://cplusplus.com/reference/deque/deque/operators/
函数
<deque>
std::relational operators (deque)
(1) template <class T, class Alloc> bool operator== (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);
(2) template <class T, class Alloc> bool operator!= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);
(3) template <class T, class Alloc> bool operator< (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);
(4) template <class T, class Alloc> bool operator<= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);
(5) template <class T, class Alloc> bool operator> (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);
(6) template <class T, class Alloc> bool operator>= (const deque<T,Alloc>& lhs, const deque<T,Alloc>& rhs);
deque的关系操作符
在deque容器lhs和rhs之间执行适当的比较操作。
相等比较(operator==)是通过第一次比较sizes来执行的,如果它们匹配,则使用operator==对元素进行顺序比较,在第一次不匹配时停止(就像使用算法equal)。
小于比较(operator<)的行为类似于使用算法lexicographical_compare
,它以互反的方式使用operator<依次比较元素(即检查a<b和b<a)并在第一次出现时停止。
其他操作也在内部使用操作符==和<来比较元素,就像执行了以下等价操作一样:
operation | equivalent operation |
---|---|
a!=b | !(a==b) |
a>b | b<a |
a<=b | !(b<a) |
a>=b | !(a<b) |
这些操作符在头文件<deque>中重载。
形参
lhs,rhs
deque容器(分别位于操作符的左边和右边),具有相同的模板形参(T和Alloc)。
用例
// deque comparisons
#include <iostream>
#include <deque>
int main ()
{
std::deque<int> foo (3,100); // three ints with a value of 100
std::deque<int> bar (2,200); // two ints with a value of 200
if (foo==bar) std::cout << "foo and bar are equal\n";
if (foo!=bar) std::cout << "foo and bar are not equal\n";
if (foo< bar) std::cout << "foo is less than bar\n";
if (foo> bar) std::cout << "foo is greater than bar\n";
if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";
return 0;
}
输出:
返回值
如果条件成立,则为true,否则为false。
复杂度
C++98
最多达到lhs和rhs的size中的线性。
C++14
对于(1)和(2),如果lhs和rhs的sizes不同,则为常量,否则最多为size(相等比较)中的线性。
对于其它的,最多达到较小的size中的线性(每一个表示具有operator<的两次比较)。
iterator的有效性
没有变化。
数据竞争
两个容器lhs和rhs都被访问。
它们所包含的所有元素都可以被访问。
异常安全
如果元素的类型支持带有无抛出保证的适当操作,则该函数永远不会抛出异常(无抛出保证)。
在任何情况下,函数都不能修改它的实参。