Operatorr操作符重载函数
语法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
1.赋值操作符重载 vector& operator=(const vector& __x); 2.下标操作符重载 reference operator[](size_type __n); 3.相等操作符重载 template<typename _Tp, typename _Alloc> inline bool operator==(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y); 4.小于操作符重载 template<typename _Tp, typename _Alloc> inline bool operator<(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y); 5.不等于操作符重载 template<typename _Tp, typename _Alloc> inline bool operator!=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y); 6.大于操作符重载 template<typename _Tp, typename _Alloc> inline bool operator>(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y); 7.小于等于操作符重载 template<typename _Tp, typename _Alloc> inline bool operator<=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y); 8.大于等于操作符重载 template<typename _Tp, typename _Alloc> inline bool operator>=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y); |
操作符重载函数
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
/** * @brief %Vector assignment operator. * @param x A %vector of identical element and allocator types. * * All the elements of @a x are copied, but any extra memory in * @a x (for fast expansion) will not be copied. Unlike the * copy constructor, the allocator object is not copied. */ vector& operator=(const vector& __x); // element access /** * @brief Subscript access to the data contained in the %vector. * @param n The index of the element for which data should be * accessed. * @return Read/write reference to data. * * This operator allows for easy, array-style, data access. * Note that data access with this operator is unchecked and * out_of_range lookups are not defined. (For checked lookups * see at().) */ reference operator[](size_type __n) { return *(begin() + __n); } /** * @brief Subscript access to the data contained in the %vector. * @param n The index of the element for which data should be * accessed. * @return Read-only (constant) reference to data. * * This operator allows for easy, array-style, data access. * Note that data access with this operator is unchecked and * out_of_range lookups are not defined. (For checked lookups * see at().) */ const_reference operator[](size_type __n) const { return *(begin() + __n); } /** * @brief Vector equality comparison. * @param x A %vector. * @param y A %vector of the same type as @a x. * @return True iff the size and elements of the vectors are equal. * * This is an equivalence relation. It is linear in the size of the * vectors. Vectors are considered equivalent if their sizes are equal, * and if corresponding elements compare equal. */ template<typename _Tp, typename _Alloc> inline bool operator==(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y) { return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin()); } /** * @brief Vector ordering relation. * @param x A %vector. * @param y A %vector of the same type as @a x. * @return True iff @a x is lexicographically less than @a y. * * This is a total ordering relation. It is linear in the size of the * vectors. The elements must be comparable with @c <. * * See std::lexicographical_compare() for how the determination is made. */ template<typename _Tp, typename _Alloc> inline bool operator<(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y) { return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); } /// Based on operator== template<typename _Tp, typename _Alloc> inline bool operator!=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y) { return !(__x == __y); } /// Based on operator< template<typename _Tp, typename _Alloc> inline bool operator>(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y) { return __y < __x; } /// Based on operator< template<typename _Tp, typename _Alloc> inline bool operator<=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y) { return !(__y < __x); } /// Based on operator< template<typename _Tp, typename _Alloc> inline bool operator>=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y) { return !(__x < __y); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1(10, 0); vector<int> v2(10, 2); if (v1 == v2) cout<<"v1向量和v2向量相等"<<endl; if (v1 != v2) cout<<"v1向量和v2向量不相等"<<endl; if (v1 <= v2) cout<<"v1向量小于等于v2向量"<<endl; if (v1 >= v2) cout<<"v1向量大于等于v2向量"<<endl; if (v1 < v2) cout<<"v1向量小于v2向量"<<endl; if (v1 > v2) cout<<"v1向量大于v2向量"<<endl; v1 = v2; //调用赋值操作符重载 for(int i=0;i<10;i++){ cout<<v1[i]<<","; } cout<<endl; for(int i=0;i<10;i++){ cout<<v2[i]<<","; } cout<<endl; } |
测试结果:
1 2 3 4 5 |
v1向量和v2向量不相等 v1向量小于等于v2向量 v1向量小于v2向量 2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2, |
注:两个vector被认为是相等的,如果:
- 它们具有相同的容量
- 所有相同位置的元素相等.
vectors之间大小的比较是按照词典规则.