From 688a027b81dd081d8aa2886d36d6096c207350e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E7=86=99=E6=9D=A5?= <617064818@qq.com> Date: Wed, 8 Jan 2025 21:23:11 +0800 Subject: [PATCH] update functional.h and util.h --- src/MyTinySTL-master/MyTinySTL/functional.h | 175 ++++-- src/MyTinySTL-master/MyTinySTL/util.h | 642 +++++++++++--------- 2 files changed, 453 insertions(+), 364 deletions(-) diff --git a/src/MyTinySTL-master/MyTinySTL/functional.h b/src/MyTinySTL-master/MyTinySTL/functional.h index e1d319d..4267b1a 100644 --- a/src/MyTinySTL-master/MyTinySTL/functional.h +++ b/src/MyTinySTL-master/MyTinySTL/functional.h @@ -1,155 +1,191 @@ #ifndef MYTINYSTL_FUNCTIONAL_H_ +// 定义一个宏,防止头文件被重复包含 #define MYTINYSTL_FUNCTIONAL_H_ +// 定义宏结束 -// 这个头文件包含了 mystl 的函数对象与哈希函数 - -#include +#include +// 包含标准库中的 cstddef 头文件,用于定义 size_t 等类型 namespace mystl { +// 定义一个命名空间 mystl,用于封装自定义的函数对象等 -// 定义一元函数的参数型别和返回值型别 -// 注意模板类的使用 template struct unarg_function { - typedef Arg argument_type; - typedef Result result_type; + typedef Arg argument_type; + // 定义一个类型别名 argument_type,表示单参数函数对象的参数类型 + typedef Result result_type; + // 定义一个类型别名 result_type,表示单参数函数对象的结果类型 }; -// 定义二元函数的参数型别的返回值型别 +// 定义一个模板结构体 unarg_function,用于表示单参数函数对象的类型特征 + template struct binary_function { - typedef Arg1 first_argument_type; - typedef Arg2 second_argument_type; - typedef Result result_type; + typedef Arg1 first_argument_type; + // 定义一个类型别名 first_argument_type,表示二元函数对象的第一个参数类型 + typedef Arg2 second_argument_type; + // 定义一个类型别名 second_argument_type,表示二元函数对象的第二个参数类型 + typedef Result result_type; + // 定义一个类型别名 result_type,表示二元函数对象的结果类型 }; -// 关于函数对象的四则运算 -// 函数对象:加法 +// 定义一个模板结构体 binary_function,用于表示二元函数对象的类型特征 + template struct plus :public binary_function { T operator()(const T& x, const T& y) const { return x + y; } + // 定义一个模板结构体 plus,继承自 binary_function,重载 operator() 实现加法操作 }; -// 函数对象:减法 +// 定义一个模板结构体 plus,表示加法函数对象 + template struct minus :public binary_function { T operator()(const T& x, const T& y) const { return x - y; } + // 定义一个模板结构体 minus,继承自 binary_function,重载 operator() 实现减法操作 }; -// 函数对象:乘法 +// 定义一个模板结构体 minus,表示减法函数对象 + template struct multiplies :public binary_function { T operator()(const T& x, const T& y) const { return x * y; } + // 定义一个模板结构体 multiplies,继承自 binary_function,重载 operator() 实现乘法操作 }; -// 函数对象:除法 +// 定义一个模板结构体 multiplies,表示乘法函数对象 + template struct divides :public binary_function { T operator()(const T& x, const T& y) const { return x / y; } + // 定义一个模板结构体 divides,继承自 binary_function,重载 operator() 实现除法操作 }; -// 函数对象:模取 +// 定义一个模板结构体 divides,表示除法函数对象 + template struct modulus :public binary_function { T operator()(const T& x, const T& y) const { return x % y; } + // 定义一个模板结构体 modulus,继承自 binary_function,重载 operator() 实现取模操作 }; -// 函数对象:否定 +// 定义一个模板结构体 modulus,表示取模函数对象 + template struct negate :public unarg_function { T operator()(const T& x) const { return -x; } + // 定义一个模板结构体 negate,继承自 unarg_function,重载 operator() 实现取反操作 }; -// 证同元素的意思是,对象与该元素做运算后,该对象仍等于自身,即零元 -// 加法的证同元素 + +// 定义一个模板结构体 negate,表示取反函数对象 + template T identity_element(plus) { return T(0); } +// 定义一个模板函数 identity_element,用于返回加法函数对象的单位元(0) -// 乘法的证同元素 template T identity_element(multiplies) { return T(1); } +// 定义一个模板函数 identity_element,用于返回乘法函数对象的单位元(1) -// 关于函数对象的逻辑运算 -// 函数对象:等于 template struct equal_to :public binary_function { bool operator()(const T& x, const T& y) const { return x == y; } + // 定义一个模板结构体 equal_to,继承自 binary_function,重载 operator() 实现等于比较 }; -// 函数对象:不等于 +// 定义一个模板结构体 equal_to,表示等于比较函数对象 + template struct not_equal_to :public binary_function { bool operator()(const T& x, const T& y) const { return x != y; } + // 定义一个模板结构体 not_equal_to,继承自 binary_function,重载 operator() 实现不等于比较 }; -// 函数对象:大于 +// 定义一个模板结构体 not_equal_to,表示不等于比较函数对象 + template struct greater :public binary_function { bool operator()(const T& x, const T& y) const { return x > y; } + // 定义一个模板结构体 greater,继承自 binary_function,重载 operator() 实现大于比较 }; -// 函数对象:小于 +// 定义一个模板结构体 greater,表示大于比较函数对象 + template struct less :public binary_function { bool operator()(const T& x, const T& y) const { return x < y; } + // 定义一个模板结构体 less,继承自 binary_function,重载 operator() 实现小于比较 }; -// 函数对象:大于等于 +// 定义一个模板结构体 less,表示小于比较函数对象 + template struct greater_equal :public binary_function { bool operator()(const T& x, const T& y) const { return x >= y; } + // 定义一个模板结构体 greater_equal,继承自 binary_function,重载 operator() 实现大于等于比较 }; -// 函数对象:小于等于 +// 定义一个模板结构体 greater_equal,表示大于等于比较函数对象 + template struct less_equal :public binary_function { bool operator()(const T& x, const T& y) const { return x <= y; } + // 定义一个模板结构体 less_equal,继承自 binary_function,重载 operator() 实现小于等于比较 }; -// 函数对象:逻辑与 +// 定义一个模板结构体 less_equal,表示小于等于比较函数对象 + template struct logical_and :public binary_function { bool operator()(const T& x, const T& y) const { return x && y; } + // 定义一个模板结构体 logical_and,继承自 binary_function,重载 operator() 实现逻辑与操作 }; -// 函数对象:逻辑或 +// 定义一个模板结构体 logical_and,表示逻辑与函数对象 + template struct logical_or :public binary_function { bool operator()(const T& x, const T& y) const { return x || y; } + // 定义一个模板结构体 logical_or,继承自 binary_function,重载 operator() 实现逻辑或操作 }; -// 函数对象:逻辑非 +// 定义一个模板结构体 logical_or,表示逻辑或函数对象 + template struct logical_not :public unarg_function { bool operator()(const T& x) const { return !x; } + // 定义一个模板结构体 logical_not,继承自 unarg_function,重载 operator() 实现逻辑非操作 }; -// 证同函数:不会改变元素,返回本身 +// 定义一个模板结构体 logical_not,表示逻辑非函数对象 + template struct identity :public unarg_function { const T& operator()(const T& x) const { return x; } + // 定义一个模板结构体 identity,继承自 unarg_function,重载 operator() 实现恒等操作 }; -// select -// 选择函数:接受一个 pair,返回第一个元素 +// 定义一个模板结构体 identity,表示恒等函数对象 + template struct selectfirst :public unarg_function { @@ -157,9 +193,11 @@ struct selectfirst :public unarg_function { return x.first; } + // 定义一个模板结构体 selectfirst,继承自 unarg_function,重载 operator() 实现选择对的第一个元素 }; -// 选择函数:接受一个 pair,返回第二个元素 +// 定义一个模板结构体 selectfirst,表示选择对的第一个元素的函数对象 + template struct selectsecond :public unarg_function { @@ -167,38 +205,48 @@ struct selectsecond :public unarg_function { return x.second; } + // 定义一个模板结构体 selectsecond,继承自 unarg_function,重载 operator() 实现选择对的第二个元素 }; -// 投射函数:返回第一参数 +// 定义一个模板结构体 selectsecond,表示选择对的第二个元素的函数对象 + template struct projectfirst :public binary_function { Arg1 operator()(const Arg1& x, const Arg2&) const { return x; } + // 定义一个模板结构体 + template +struct projectfirst :public binary_function +{ + Arg1 operator()(const Arg1& x, const Arg2&) const { return x; } + // 定义一个模板结构体 projectfirst,继承自 binary_function,重载 operator() 实现返回第一个参数 }; -// 投射函数:返回第二参数 +// 定义一个模板结构体 projectfirst,表示返回第一个参数的函数对象 + template -struct projectsecond :public binary_function +struct projectsecond :public binary_function { Arg2 operator()(const Arg1&, const Arg2& y) const { return y; } + // 定义一个模板结构体 projectsecond,继承自 binary_function,重载 operator() 实现返回第二个参数 }; -/*****************************************************************************************/ -// 哈希函数对象 +// 定义一个模板结构体 projectsecond,表示返回第二个参数的函数对象 -// 对于大部分类型,hash function 什么都不做 template struct hash {}; +// 定义一个模板结构体 hash,用于定义哈希函数的接口 -// 针对指针的偏特化版本 template struct hash { size_t operator()(T* p) const noexcept { return reinterpret_cast(p); } + // 为指针类型特化 hash 结构体,重载 operator() 实现指针的哈希运算 }; -// 对于整型类型,只是返回原值 +// 为指针类型特化 hash 结构体 + #define MYSTL_TRIVIAL_HASH_FCN(Type) \ template <> struct hash \ { \ @@ -206,42 +254,28 @@ template <> struct hash \ { return static_cast(val); } \ }; -// 关于其他类型数据的HASH函数 +// 定义一个宏 MYSTL_TRIVIAL_HASH_FCN,用于为基本类型生成简单的哈希函数 MYSTL_TRIVIAL_HASH_FCN(bool) - MYSTL_TRIVIAL_HASH_FCN(char) - MYSTL_TRIVIAL_HASH_FCN(signed char) - MYSTL_TRIVIAL_HASH_FCN(unsigned char) - MYSTL_TRIVIAL_HASH_FCN(wchar_t) - MYSTL_TRIVIAL_HASH_FCN(char16_t) - MYSTL_TRIVIAL_HASH_FCN(char32_t) - MYSTL_TRIVIAL_HASH_FCN(short) - MYSTL_TRIVIAL_HASH_FCN(unsigned short) - MYSTL_TRIVIAL_HASH_FCN(int) - MYSTL_TRIVIAL_HASH_FCN(unsigned int) - MYSTL_TRIVIAL_HASH_FCN(long) - MYSTL_TRIVIAL_HASH_FCN(unsigned long) - MYSTL_TRIVIAL_HASH_FCN(long long) - MYSTL_TRIVIAL_HASH_FCN(unsigned long long) +// 使用宏为各种基本类型生成简单的哈希函数 #undef MYSTL_TRIVIAL_HASH_FCN +// 取消宏定义 -// 对于浮点数,逐位哈希 -// 逐位哈希函数 inline size_t bitwise_hash(const unsigned char* first, size_t count) { #if (_MSC_VER && _WIN64) || ((__GNUC__ || __clang__) &&__SIZEOF_POINTER__ == 8) @@ -259,8 +293,8 @@ inline size_t bitwise_hash(const unsigned char* first, size_t count) } return result; } +// 定义一个函数 bitwise_hash,用于计算给定字节序列的哈希值,使用 FNV 哈希算法 -// 单精度浮点 template <> struct hash { @@ -268,28 +302,35 @@ struct hash { return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(float)); } + // 为 float 类型特化 hash 结构体,重载 operator() 实现 float 的哈希运算 }; -// 多精度浮点 +// 为 float 类型特化 hash 结构体 + template <> struct hash { size_t operator()(const double& val) { - return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(double)); + return val == 0.0 ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(double)); } + // 为 double 类型特化 hash 结构体,重载 operator() 实现 double 的哈希运算 }; -// 高精度浮点 +// 为 double 类型特化 hash 结构体 + template <> struct hash { size_t operator()(const long double& val) { - return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(long double)); + return val == 0.0 ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(long double)); } + // 为 long double 类型特化 hash 结构体,重载 operator() 实现 long double 的哈希运算 }; -} // namespace mystl -#endif // !MYTINYSTL_FUNCTIONAL_H_ +// 为 long double 类型特化 hash 结构体 +} +#endif +// 结束命名空间 mystl 和条件编译宏定义 \ No newline at end of file diff --git a/src/MyTinySTL-master/MyTinySTL/util.h b/src/MyTinySTL-master/MyTinySTL/util.h index df6d583..aac5bf1 100644 --- a/src/MyTinySTL-master/MyTinySTL/util.h +++ b/src/MyTinySTL-master/MyTinySTL/util.h @@ -1,297 +1,345 @@ -#ifndef MYTINYSTL_UTIL_H_ -#define MYTINYSTL_UTIL_H_ - -// 这个文件包含一些通用工具,包括 move, forward, swap 等函数,以及 pair 等 - -#include - -#include "type_traits.h" - -namespace mystl -{ - -// move - -template -typename std::remove_reference::type&& move(T&& arg) noexcept -{ - return static_cast::type&&>(arg); -} - -// forward - -template -T&& forward(typename std::remove_reference::type& arg) noexcept -{ - return static_cast(arg); -} - -template -T&& forward(typename std::remove_reference::type&& arg) noexcept -{ - static_assert(!std::is_lvalue_reference::value, "bad forward"); - return static_cast(arg); -} - -// swap - -template -void swap(Tp& lhs, Tp& rhs) -{ - auto tmp(mystl::move(lhs)); - lhs = mystl::move(rhs); - rhs = mystl::move(tmp); -} - -template -ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2) -{ - for (; first1 != last1; ++first1, (void) ++first2) - mystl::swap(*first1, *first2); - return first2; -} - -template -void swap(Tp(&a)[N], Tp(&b)[N]) -{ - mystl::swap_range(a, a + N, b); -} - -// -------------------------------------------------------------------------------------- -// pair - -// 结构体模板 : pair -// 两个模板参数分别表示两个数据的类型 -// 用 first 和 second 来分别取出第一个数据和第二个数据 -template -struct pair -{ - typedef Ty1 first_type; - typedef Ty2 second_type; - - first_type first; // 保存第一个数据 - second_type second; // 保存第二个数据 - - // default constructiable - template ::value && - std::is_default_constructible::value, void>::type> - constexpr pair() - : first(), second() - { - } - - // implicit constructiable for this type - template ::value && - std::is_copy_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(const Ty1& a, const Ty2& b) - : first(a), second(b) - { - } - - // explicit constructible for this type - template ::value && - std::is_copy_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(const Ty1& a, const Ty2& b) - : first(a), second(b) - { - } - - pair(const pair& rhs) = default; - pair(pair&& rhs) = default; - - // implicit constructiable for other type - template ::value && - std::is_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(Other1&& a, Other2&& b) - : first(mystl::forward(a)), - second(mystl::forward(b)) - { - } - - // explicit constructiable for other type - template ::value && - std::is_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(Other1&& a, Other2&& b) - : first(mystl::forward(a)), - second(mystl::forward(b)) - { - } - - // implicit constructiable for other pair - template ::value && - std::is_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(const pair& other) - : first(other.first), - second(other.second) - { - } - - // explicit constructiable for other pair - template ::value && - std::is_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(const pair& other) - : first(other.first), - second(other.second) - { - } - - // implicit constructiable for other pair - template ::value && - std::is_constructible::value && - std::is_convertible::value && - std::is_convertible::value, int>::type = 0> - constexpr pair(pair&& other) - : first(mystl::forward(other.first)), - second(mystl::forward(other.second)) - { - } - - // explicit constructiable for other pair - template ::value && - std::is_constructible::value && - (!std::is_convertible::value || - !std::is_convertible::value), int>::type = 0> - explicit constexpr pair(pair&& other) - : first(mystl::forward(other.first)), - second(mystl::forward(other.second)) - { - } - - // copy assign for this pair - pair& operator=(const pair& rhs) - { - if (this != &rhs) - { - first = rhs.first; - second = rhs.second; - } - return *this; - } - - // move assign for this pair - pair& operator=(pair&& rhs) - { - if (this != &rhs) - { - first = mystl::move(rhs.first); - second = mystl::move(rhs.second); - } - return *this; - } - - // copy assign for other pair - template - pair& operator=(const pair& other) - { - first = other.first; - second = other.second; - return *this; - } - - // move assign for other pair - template - pair& operator=(pair&& other) - { - first = mystl::forward(other.first); - second = mystl::forward(other.second); - return *this; - } - - ~pair() = default; - - void swap(pair& other) - { - if (this != &other) - { - mystl::swap(first, other.first); - mystl::swap(second, other.second); - } - } - -}; - -// 重载比较操作符 -template -bool operator==(const pair& lhs, const pair& rhs) -{ - return lhs.first == rhs.first && lhs.second == rhs.second; -} - -template -bool operator<(const pair& lhs, const pair& rhs) -{ - return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second); -} - -template -bool operator!=(const pair& lhs, const pair& rhs) -{ - return !(lhs == rhs); -} - -template -bool operator>(const pair& lhs, const pair& rhs) -{ - return rhs < lhs; -} - -template -bool operator<=(const pair& lhs, const pair& rhs) -{ - return !(rhs < lhs); -} - -template -bool operator>=(const pair& lhs, const pair& rhs) -{ - return !(lhs < rhs); -} - -// 重载 mystl 的 swap -template -void swap(pair& lhs, pair& rhs) -{ - lhs.swap(rhs); -} - -// 全局函数,让两个数据成为一个 pair -template -pair make_pair(Ty1&& first, Ty2&& second) -{ - return pair(mystl::forward(first), mystl::forward(second)); -} - -} - -#endif // !MYTINYSTL_UTIL_H_ - +#ifndef MYTINYSTL_UTIL_H_// +// 防止头文件被重复包含的宏定义开始// +#define MYTINYSTL_UTIL_H_// +// 防止头文件被重复包含的宏定义结束// +// +#include // +// 包含标准库中的cstddef头文件,通常用于定义一些基本类型如size_t等// +// +#include "type_traits.h"// +// 包含自定义的type_traits头文件,用于提供类型特征支持// +// +namespace mystl// +// 定义命名空间mystl,用于封装自定义的STL实现// +// +{// +// +template // +typename std::remove_reference::type&& move(T&& arg) noexcept// +// 定义move函数模板,用于将参数arg转换为右值引用// +{// + return static_cast::type&&>(arg);// + // 使用static_cast将arg转换为右值引用,并返回// +}// +// +template // +T&& forward(typename std::remove_reference::type& arg) noexcept// +// 定义forward函数模板的重载版本,用于完美转发左值引用// +{// + return static_cast(arg);// + // 使用static_cast将arg转换为T类型的右值引用,并返回// +}// +// +template // +T&& forward(typename std::remove_reference::type&& arg) noexcept// +// 定义forward函数模板的重载版本,用于完美转发右值引用// +{// + static_assert(!std::is_lvalue_reference::value, "bad forward");// + // 静态断言,确保T不是左值引用类型,否则编译报错// + return static_cast(arg);// + // 使用static_cast将arg转换为T类型的右值引用,并返回// +}// +// +template // +void swap(Tp& lhs, Tp& rhs)// +// 定义swap函数模板,用于交换两个变量的值// +{// + auto tmp(mystl::move(lhs));// + // 使用move将lhs转换为右值引用,并存储在tmp中// + lhs = mystl::move(rhs);// + // 将rhs的值赋给lhs// + rhs = mystl::move(tmp);// + // 将tmp的值赋给rhs// +}// +// +template // +ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2)// +// 定义swap_range函数模板,用于交换两个范围内的元素// +{// + for (; first1 != last1; ++first1, (void) ++first2)// + // 遍历两个范围内的元素// + mystl::swap(*first1, *first2);// + // 交换对应位置的元素// + return first2;// + // 返回第二个范围的结束迭代器// +}// +// +template // +void swap(Tp(&a)[N], Tp(&b)[N])// +// 定义swap函数模板的重载版本,用于交换两个固定大小数组的值// +{// + mystl::swap_range(a, a + N, b);// + // 使用swap_range函数交换两个数组的元素// +}// +// +template // +struct pair// +// 定义pair结构体模板,用于存储两个值// +{// + typedef Ty1 first_type;// + typedef Ty2 second_type;// + // 定义类型别名,分别表示第一个和第二个值的类型// +// + first_type first; // + second_type second; // + // 定义两个成员变量,分别存储第一个和第二个值// +// +// + template ::value &&// + std::is_default_constructible::value, void>::type>// + constexpr pair()// + : first(), second()// + // 定义默认构造函数,使用默认构造方式初始化first和second// + {// + }// +// + template ::value &&// + std::is_copy_constructible::value &&// + std::is_convertible::value &&// + std::is_convertible::value, int>::type = 0>// + constexpr pair(const Ty1& a, const Ty2& b)// + : first(a), second(b)// + // 定义拷贝构造函数,使用拷贝构造方式初始化first和second// + {// + }// +// + template ::value &&// + std::is_copy_constructible::value &&// + (!std::is_convertible::value ||// + !std::is_convertible::value), int>::type = 0>// + explicit constexpr pair(const Ty1& a, const Ty2& b)// + : first(a), second(b)// + // 定义显式拷贝构造函数,使用拷贝构造方式初始化first和second// + {// + }// +// + pair(const pair& rhs) = default;// + // 默认拷贝构造函数// +// + pair(pair&& rhs) = default;// + // 默认移动构造函数// +// + template ::value &&// + std::is_constructible::value &&// + std::is_convertible::value &&// + std::is_convertible::value, int>::type = 0>// + constexpr pair(Other1&& a, Other2&& b)// + : first(mystl::forward(a)),// + second(mystl::forward(b))// + // 定义构造函数,使用完美转发初始化first和second// + {// + }// +// + template ::value &&// + std::is_constructible::value &&// + (!std::is_convertible::value ||// + !std::is_convertible::value), int>::type = 0>// + explicit constexpr pair(Other1&& a, Other2&& b)// + : first(mystl::forward(a)),// + second(mystl::forward(b))// + // 定义显式构造函数,使用完美转发初始化first和second// + {// + }// +// + template ::value &&// + std::is_constructible::value &&// + std::is_convertible::value &&// + std::is_convertible::value, int>::type = 0>// + constexpr pair(const pair& other)// + : first(other.first),// + second(other.second)// + // 定义拷贝构造函数,使用拷贝构造方式初始化first和second// + {// + }// +// + template ::value &&// + std::is_constructible::value &&// + (!std::is_convertible::value ||// + !std::is_convertible::value), int>::type = 0>// + explicit constexpr pair(const pair::value &&// + std::is_constructible::value &&// + std::is_convertible::value &&// + std::is_convertible::value, int>::type = 0>// + constexpr pair(pair&& other)// + : first(mystl::forward(other.first)),// + second(mystl::forward(other.second))// + // 定义构造函数,使用完美转发初始化first和second// + {// + }// +// + template ::value &&// + std::is_constructible::value &&// + (!std::is_convertible::value ||// + !std::is_convertible::value), int>::type = 0>// + explicit constexpr pair(pair&& other)// + : first(mystl::forward(other.first)),// + second(mystl::forward(other.second))// + // 继续使用完美转发初始化second成员变量// + {// + }// +// + pair& operator=(const pair& rhs)// + // 定义拷贝赋值运算符// + {// + if (this != &rhs)// + // 如果不是自赋值// + {// + first = rhs.first;// + // 将rhs的first赋值给当前对象的first// + second = rhs.second;// + // 将rhs的second赋值给当前对象的second// + }// + return *this;// + // 返回当前对象的引用// + }// +// + pair& operator=(pair&& rhs)// + // 定义移动赋值运算符// + {// + if (this != &rhs)// + // 如果不是自赋值// + {// + first = mystl::move(rhs.first);// + // 将rhs的first移动赋值给当前对象的first// + second = mystl::move(rhs.second);// + // 将rhs的second移动赋值给当前对象的second// + }// + return *this;// + // 返回当前对象的引用// + }// +// + template // + pair& operator=(const pair& other)// + // 定义模板化的拷贝赋值运算符// + {// + first = other.first;// + // 将other的first赋值给当前对象的first// + second = other.second;// + // 将other的second赋值给当前对象的second// + return *this;// + // 返回当前对象的引用// + }// +// + template // + pair& operator=(pair&& other)// + // 定义模板化的移动赋值运算符// + {// + first = mystl::forward(other.first);// + // 将other的first移动赋值给当前对象的first// + second = mystl::forward(other.second);// + // 将other的second移动赋值给当前对象的second// + return *this;// + // 返回当前对象的引用// + }// +// + ~pair() = default;// + // 默认析构函数// +// + void swap(pair& other)// + // 定义成员函数swap,用于交换两个pair对象的成员变量// + {// + if (this != &other)// + // 如果不是自交换// + {// + mystl::swap(first, other.first);// + // 交换first成员变量// + mystl::swap(second, other.second);// + // 交换second成员变量// + }// + }// +// +};// +// +template // +bool operator==(const pair& lhs, const pair& rhs)// +// 定义全局函数operator==,用于比较两个pair对象是否相等// +{// + return lhs.first == rhs.first && lhs.second == rhs.second;// + // 如果两个pair对象的first和second都相等,则认为两个pair对象相等// +}// +// +template // +bool operator<(const pair& lhs, const pair& rhs)// +// 定义全局函数operator<,用于比较两个pair对象的大小// +{// + return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second);// + // 如果lhs的first小于rhs的first,或者lhs的first等于rhs的first且lhs的second小于rhs的second,则认为lhs小于rhs// +}// +// +template // +bool operator!=(const pair& lhs, const pair& rhs)// +// 定义全局函数operator!=,用于比较两个pair对象是否不相等// +{// + return !(lhs == rhs);// + // 如果两个pair对象不相等,则返回true// +}// +// +template // +bool operator>(const pair& lhs, const pair& rhs)// +// 定义全局函数operator>,用于比较两个pair对象的大小// +{// + return rhs < lhs;// + // 如果rhs小于lhs,则认为lhs大于rhs// +}// +// +template // +bool operator<=(const pair& lhs, const pair& rhs)// +// 定义全局函数operator<=,用于比较两个pair对象的大小// +{// + return !(rhs < lhs);// + // 如果rhs不小于lhs,则认为lhs小于等于rhs// +}// +// +template // +bool operator>=(const pair& lhs, const pair& rhs)// +// 定义全局函数operator>=,用于比较两个pair对象的大小// +{// + return !(lhs < rhs);// + // 如果lhs不小于rhs,则认为lhs大于等于rhs// +}// +// +template // +void swap(pair& lhs, pair& rhs)// +// 定义全局函数swap,用于交换两个pair对象的成员变量// +{// + lhs.swap(rhs);// + // 调用pair对象的成员函数swap进行交换// +}// +// +template // +pair make_pair(Ty1&& first, Ty2&& second)// +// 定义全局函数make_pair,用于创建pair对象// +{// + return pair(mystl::forward(first), mystl::forward(second));// + // 使用完美转发创建pair对象并返回// +}// +// +} // namespace mystl// +// +#endif // MYTINYSTL_UTIL_H_// +// 防止头文件被重复包含的宏定义结束//