std::
relational operators (list)
函数模板 <list>
(1) |
template <class T, class Alloc>
bool operator== (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
(2) |
template <class T, class Alloc>
bool operator!= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
(3) |
template <class T, class Alloc>
bool operator< (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
(4) |
template <class T, class Alloc>
bool operator<= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
(5) |
template <class T, class Alloc>
bool operator> (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
(6) |
template <class T, class Alloc>
bool operator>= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
list的关系操作符
在list容器lhs和rhs之间执行相应的比较操作。
相等比较(operator==)通过首先比较大小执行,
如果匹配,则使用operator==顺序比较元素,在第一次不匹配时停止(就像使用algorithm equal一样)。
小于比较(operator<)的行为就像使用算法lexicographal_compare,
它使用operator<以对等的方式顺序比较元素(即,检查a<b和b<a),并在第一次出现时停止。
其他操作也在内部使用操作符"=="和"<"来比较元素,就像执行了以下等价操作一样:
操作符 |
等价操作 |
a!=b |
!(a==b) |
a>b |
b<a |
a<=b |
!(b<a) |
a>=b |
!(a<b) |
这些操作符在头文件<list>中重载.
☲ 参数
-
lhs, rhs
-
具有相同模板形参(T和Alloc)的list容器(分别位于操作符的左边和右边)。
☉ 返回值
如果条件成立,则为真,否则为假。
☣ 示例
// list comparisons
#include <iostream>
#include <list>
int main ()
{
std::list<int> a = {10, 20, 30};
std::list<int> b = {10, 20, 30};
std::list<int> c = {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
✥ 复杂度
c++98: 在lhs和rhs的大小上达到线性。
c++11: 对于(1)和(2),如果lhs和rhs的大小不同,则为常数,否则为线性(相等比较)。
对于其他运算符,最小值为线性(每个运算符表示两个运算符<的比较)。
☣ 迭代器的有效性
不变
⇄ 数据竞争
两个容器(lhs和rhs)都被访问。
它们所包含的所有元素都可以被访问。
☂ 异常安全性
如果元素的类型支持有no-throw保证的适当操作,则函数永远不会抛出异常(no-throw保证)。
在任何情况下,函数都不能修改它的参数。
🍄 另请参阅
list::swap |
交换内容(公众成员函数) |
list::unique |
删除重复的值(公众成员函数) |