std :: vector运算符重载不带常量参数?

时间:2013-06-04 08:06:47

标签: c++

为什么this page上的所有运算符重载都会引用std::vector但不引用const引用?他们没有修改向量,为什么不const

2 个答案:

答案 0 :(得分:5)

看起来像是对该页面的疏忽。从c ++ 11标准的 23.3.6.1类模板向量概述开始,它们都采用const& s:

template <class T, class Allocator>
bool operator==(const vector<T,Allocator>& x,
                const vector<T,Allocator>& y);

template <class T, class Allocator>
bool operator< (const vector<T,Allocator>& x,
                const vector<T,Allocator>& y);

template <class T, class Allocator>
bool operator!=(const vector<T,Allocator>& x,
                const vector<T,Allocator>& y);

template <class T, class Allocator>
bool operator> (const vector<T,Allocator>& x,
                const vector<T,Allocator>& y);

template <class T, class Allocator>
bool operator>=(const vector<T,Allocator>& x,
                const vector<T,Allocator>& y);

template <class T, class Allocator>
bool operator<=(const vector<T,Allocator>& x,
                const vector<T,Allocator>& y);

答案 1 :(得分:2)

根据this page,他们将const引用作为参数。根据C ++ 11标准的第23.3.6.1节和C ++ 03标准的第23.2.4节,它们也是如此。