STL源码分析——字符特性类模板char_traits

字符特性模板,目的是提供最基本的字符特性的统一的方法函数。
char_traits继承了__char_traits_base

// Class __char_traits_base.

// 字符特性基类模板

template <class _CharT, class _IntT> class __char_traits_base {

public:

	typedef _CharT char_type;   // 字符类型

	typedef _IntT int_type;     // int类型

#ifdef __STL_USE_NEW_IOSTREAMS

	typedef streamoff off_type;

	typedef streampos pos_type;

	typedef mbstate_t state_type;

#endif /* __STL_USE_NEW_IOSTREAMS */



	// 字符赋值

	static void assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }



	// 字符比较

	static bool eq(const _CharT& __c1, const _CharT& __c2)

	{
		return __c1 == __c2;
	}



	// 字符大小比较

	static bool lt(const _CharT& __c1, const _CharT& __c2)

	{
		return __c1 < __c2;
	}



	// 字串比较

	static int compare(const _CharT* __s1, const _CharT* __s2, size_t __n) {

		for (size_t __i = 0; __i < __n; ++__i)

			if (!eq(__s1[__i], __s2[__i]))

				return __s1[__i] < __s2[__i] ? -1 : 1;

		return 0;

	}



	// 字串长度

	static size_t length(const _CharT* __s) {

		const _CharT __nullchar = _CharT(); //char()就是'\0'

		size_t __i;

		for (__i = 0; !eq(__s[__i], __nullchar); ++__i)

		{
		}

		return __i;

	}



	// 在串中查找字符

	static const _CharT* find(const _CharT* __s, size_t __n, const _CharT& __c)

	{

		for (; __n > 0; ++__s, --__n)

			if (eq(*__s, __c))

				return __s;

		return 0;

	}



	// 字符串移到另一字符串

	static _CharT* move(_CharT* __s1, const _CharT* __s2, size_t __n) {

		memmove(__s1, __s2, __n * sizeof(_CharT));

		return __s1;

	}



	//拷贝一字符串到另字符串

	static _CharT* copy(_CharT* __s1, const _CharT* __s2, size_t __n) {

		memcpy(__s1, __s2, __n * sizeof(_CharT));

		return __s1;

	}


	// 字符串赋值

	static _CharT* assign(_CharT* __s, size_t __n, _CharT __c) {

		for (size_t __i = 0; __i < __n; ++__i)

			__s[__i] = __c;

		return __s;

	}



	// 判断是否为结束符

	static int_type not_eof(const int_type& __c) {

		return !eq_int_type(__c, eof()) ? __c : 0;

	}



	// int到char类型的转换

	static char_type to_char_type(const int_type& __c) {

		return static_cast<char_type>(__c);

	}



	// char到int类型的转换

	static int_type to_int_type(const char_type& __c) {

		return static_cast<int_type>(__c);

	}



	// 判断俩int类型是否相等

	static bool eq_int_type(const int_type& __c1, const int_type& __c2) {

		return __c1 == __c2;
	}



	// 返回结束整型值

	static int_type eof() {

		return static_cast<int_type>(-1);
	}

};
template <class _CharT> class my_char_traits

	: public __char_traits_base<_CharT, _CharT>

{};




// Specialization for char.

// char字符类型模板偏特化
template<> class my_char_traits<char>

	: public __char_traits_base<char, int>

{

public:

	//int类型到char类型的转换

	static char_type to_char_type(const int_type& __c) {
		return static_cast<char_type>(static_cast<unsigned char>(__c));

	}

	//char到int类型的转换
	static int_type to_int_type(const char_type& __c) {

		return static_cast<unsigned char>(__c);

	}

	//比较两个字符串
	static int compare(const char* __s1, const char* __s2, size_t __n)

	{
		return memcmp(__s1, __s2, __n);
	}

	//字符串的长度
	static size_t length(const char* __s) { return strlen(__s); }

	//字符赋值

	static void assign(char& __c1, const char& __c2) { __c1 = __c2; }

	//字串赋值

	static char* assign(char* __s, size_t __n, char __c)

	{
		memset(__s, __c, __n); return __s;
	}

};



	// Specialization for wchar_t.
	// 宽字符类型模板偏特化
	template<> class my_char_traits<wchar_t>
		: public __char_traits_base<wchar_t, wint_t>
	{
		//...这里省略
	};

		int main()
		{

			my_char_traits<char>obj; 
			cout << obj.length("abc") << endl;

			system("pause");
			return 0;
		}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值