2

为什么此页面上的所有运算符重载都引用std::vector而不const引用?他们没有修改向量,为什么不const呢?

4

2 回答 2

5

看起来像是对该页面的疏忽。从第23.3.6.1节C++11 标准的类模板向量概述中,它们都采用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);
于 2013-06-04T08:13:16.257 回答
2

根据这个页面,他们确实将 const 引用作为参数。根据 C++ 11 标准的 § 23.3.6.1 和 C++ 03 标准的 § 23.2.4,它们也是如此。

于 2013-06-04T08:09:55.613 回答