继承std::string实现CString的部份方法

#include <string>
#include <stdarg.h>
using namespace std;

template<typename _CharT, typename _Traits, typename _Alloc>
class SString: public basic_string<_CharT, _Traits, _Alloc>
{
public:
	typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;

	typedef typename _CharT_alloc_type::size_type size_type;

	static const size_type npos = static_cast<size_type> (-1);


	inline SString() :
	basic_string<_CharT, _Traits, _Alloc> ()
	{
	}
	explicit SString(const _Alloc& __a) :
	basic_string<_CharT, _Traits, _Alloc> ()
	{
	}

	SString(const SString& __str) :
	basic_string<_CharT, _Traits, _Alloc> (__str)
	{
	}

	SString(const SString& __str, size_type __pos, size_type __n = npos) :
	basic_string<_CharT, _Traits, _Alloc> (__str, __pos, npos)
	{
	}

	SString(const SString& __str, size_type __pos, size_type __n,
		const _Alloc& __a) :
	basic_string<_CharT, _Traits, _Alloc> (__str, __pos, __n, __a)
	{
	}

	SString(const _CharT* __s, size_type __n, const _Alloc& __a = _Alloc()) :
	basic_string<_CharT, _Traits, _Alloc> (__s, __n, __a)
	{
	}

	SString(const _CharT* __s, const _Alloc& __a = _Alloc()) :
	basic_string<_CharT, _Traits, _Alloc> (__s, __a)
	{
	}

	SString(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) :
	basic_string<_CharT, _Traits, _Alloc> (__n, __c, __a)
	{
	}

	template<class _InputIterator>
	SString(_InputIterator __beg, _InputIterator __end, const _Alloc& __a =
		_Alloc()) :
	basic_string<_CharT, _Traits, _Alloc> (__beg, __end, __a)
	{
	}

	SString& operator=(const SString& __str)
	{
		//return this->assign(__str);
		this->assign(__str);
		return *this;
	}

	SString& operator=(const basic_string& __str)
	{
		//return this->assign(__str);
		this->assign(__str);
		return *this;
	}
	
	SString& operator=(const _CharT* __s)
	{
		//return this->assign(__s);
		this->assign(__s);
		return *this;
	}

	SString& operator=(_CharT __c)
	{
		this->assign(1, __c);
		return *this;
	}

	inline SString &Format(const char *_format, ...)
	{
		char szBuffer[1000];//这里决定了显示内容的上限。
		memset(szBuffer, 0x00, sizeof(szBuffer));

		va_list ap;
		va_start(ap, _format);

		try
		{
			vsnprintf(szBuffer, 1000, _format, ap);
		} catch (...)
		{
			//cout << "ERROR: format the string failed..." << endl;
			return *this;
		}

		va_end(ap);
		*this = szBuffer;
		return *this;
	}

	inline SString& TrimRight()
	{
		string::size_type index = this->find_last_not_of(" \n\r\t");
		if (index != string::npos){ *this = this->substr(0, index + 1);}
		return *this;
	}

	inline SString& TrimLeft()
	{
		string::size_type index = this->find_first_not_of(" \n\r\t");
		if (index != string::npos){ *this = this->substr(index);}
		return  *this;
	}

	inline SString& Trim()
	{
		TrimLeft();
		TrimRight();
		return  *this;
	}

};
typedef SString<char, char_traits<char> , allocator<char> > CString;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值