1.std::basic_string
描述:
The basic_string object keeps an internal copy of alloc, which is used to allocate and free storage for the characters it contains.
说明std::basic_string适用存储任意类型字符
windows中用它定义string和wstring类型:
using string = basic_string<char, char_traits<char>, allocator<char>>;
using wstring = basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>;
头文件
#include
模板
template <class _Elem, class _Traits, class _Alloc>
如果实现比较运算符 "<“可以按下面这么干(windows已经实现) ; 其他模板函数实现请查看标准库SDK中的"xstring.h”
template <class _Elem, class _Traits, class _Alloc>
_NODISCARD bool operator<(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) {
return _Left.compare(_Right) < 0;
}
2.实践运用:自定义字符串
//自定义字符类型
typedef char MyAChar;
typedef wchar_t MyWChar;
// linux和windows区分
#ifdef __linux__
typedef MyAChar MyChar;
#else defined(_WIN32)
typedef MyWchar MyChar;
#endif
// 使用std::basic_string自定义字符串类型
typedef std::basic_string<MyAChar> MyAString;
typedef std::basic_string<MyWChar> MyWString;
typedef std::basic_string<MyChar> MyString;