为什么this page上的所有运算符重载都会引用std::vector
但不引用const
引用?他们没有修改向量,为什么不const
?
答案 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节,它们也是如此。