在实际开发过程中,C++string类使用起来有很多不方便的地方,笔者根据根据这些不足简单的扩展了这个类,如增加与数字之间的相互转化和格式化字符串。不足的地方望指正。读者也可以根据自己需求继续扩展。
头文件:exstring.h
- #pragma once
-
- #include <string>
- #include <stdarg.h>
- #include <algorithm>
- #include <sstream>
- #include <iomanip>
-
- using namespace std;
-
- #ifndef INLINE
- #define INLINE inline
- #endif //INLINE
-
- static ios_base::fmtflags BaseFlag(int base)
- {
- return (base == 16) ? (ios_base::hex) :
- ( (base == 8) ? (ios_base::oct) : (ios_base::dec) );
- }
-
- template<class _Elem> struct ex_char_traits
- {
- };
-
- template<> struct ex_char_traits<char>
- {
- static INLINE int ct_vscprintf(const char* format, va_list argptr )
- {
- return _vscprintf(format, argptr);
- }
- static INLINE int ct_vstprintf_s(char* buffer, size_t numberOfElements,
- const char* format, va_list argptr)
- {
- return vsprintf_s(buffer, numberOfElements, format, argptr);
- }
- };
-
- template<> struct ex_char_traits<wchar_t>
- {
- static INLINE int ct_vscprintf(const wchar_t* format, va_list argptr )
- {
- return _vscwprintf(format, argptr);
- }
- static INLINE int ct_vstprintf_s(wchar_t* buffer, size_t numberOfElements,
- const wchar_t* format, va_list argptr)
- {
- return vswprintf_s(buffer, numberOfElements, format, argptr);
- }
- };
-
- template<class _Elem, class _Traits, class _Ax, class Type>
- Type ConvertToNumber(basic_stringstream<_Elem, _Traits, _Ax>& ss,
- Type t, int base)
- {
- ss.setf(BaseFlag(base), ios_base::basefield);
- ss >> t;
- return t;
- }
-
- template<class _Elem, class _Traits, class _Ax>
- float ConvertToNumber(basic_stringstream<_Elem, _Traits, _Ax>& ss,
- float t, int
- {
- ss >> t;
- return t;
- }
-
- template<class _Elem, class _Traits, class _Ax>
- double ConvertToNumber(basic_stringstream<_Elem, _Traits, _Ax>& ss,
- double t, int
- {
- ss >> t;
- return t;
- }
-
- template<class _Elem, class _Traits, class _Ax, class _ExTraits>
- class basic_exstring : public basic_string<_Elem, _Traits, _Ax>
- {
- public:
- typedef basic_exstring<_Elem, _Traits, _Ax, _ExTraits> _Myt;
- typedef basic_string<_Elem, _Traits, _Ax> _Mybase;
-
- #pragma region "constructor"
-
-
-
- explicit INLINE _Myt(const _Ax& al = _Ax())
- :_Mybase(al)
- {
- }
- INLINE _Myt(const _Myt& rhs)
- :_Mybase(rhs)
- {
- }
- INLINE _Myt(const _Myt& rhs, size_type pos, size_type n,const _Ax& al = _Ax())
- :_Mybase(rhs, pos, n, al)
- {
- }
- INLINE _Myt(const _Elem *s, size_type n, const _Ax& al = _Ax())
- :_Mybase(s, n, al)
- {
- }
- INLINE _Myt(const _Elem *s, const _Ax& al = _Ax())
- :_Mybase(s, al)
- {
- }
- INLINE _Myt(size_type n, _Elem c, const _Ax& al = _Ax())
- :_Mybase(n, c, al)
- {
- }
- INLINE _Myt(const_iterator first, const_iterator last,const _Ax& al = _Ax())
- :_Mybase(first, last, al)
- {
- }
-
-
- INLINE _Myt(const _Mybase& base)
- :_Mybase(base)
- {
-
- }
- #pragma endregion //constructor
-
-
- #pragma region "general operation"
-
-
- _Myt& toUpper()
- {
- transform(begin(), end(), begin(), toupper);
- return *this;
- }
-
-
- _Myt toUpper() const
- {
- _Myt s;
- transform(begin(), end(), s.begin(), toupper);
- return s;
- }
-
-
- _Myt& toLower()
- {
- transform(begin(), end(), begin(), tolower);
- return *this;
- }
-
-
- _Myt toLower() const
- {
- _Myt s(_Mysize, _Elem());
- transform(begin(), end(), s.begin(), tolower);
- return s;
- }
-
-
- _Myt& replace(const _Myt& oldStr, const _Myt& newStr)
- {
- if (oldStr.empty())
- return *this;
- size_type index;
- while ( (index = find(oldStr)) != npos )
- _Mybase::replace(index, oldStr.size(), newStr);
- return *this;
- }
-
-
- _Myt& trimLeft(const _Myt& target)
- {
- while (!empty() && (target.find(*begin()) != npos))
- erase(begin());
- return *this;
- }
-
-
- _Myt& trimRight(const _Myt& target)
- {
- while (!empty() && target.find(*rbegin()) != npos)
- erase(--end());
- return *this;
- }
-
-
- _Myt left(size_type count) const
- {
- return substr( 0, count );
- }
-
-
- _Myt right(size_type count) const
- {
- return substr( _Mysize < count ? 0 : _Mysize - count );
- }
-
-
- int compareNoCase(const _Myt& rhs) const
- {
- return toLower().compare(rhs.toLower());
- }
-
-
- bool beginWith(const _Myt& rhs) const
- {
- return find(rhs) == size_type(0);
- }
-
-
- bool endWith(const _Myt& rhs) const
- {
- if(rhs.size() > _Mysize)
- return false;
- return compare(_Mysize - rhs.size(), rhs.size(), rhs) == 0;
- }
- #pragma endregion //general operation
-
-
- #pragma region "convert between numbers"
-
-
-
- template<typename T>
- T toNumber (int base = 10) const
- {
- T t = T();
- basic_stringstream<_Elem, _Traits, _Ax> ss(_Myptr());
- return ConvertToNumber<_Elem, _Traits, _Ax>(ss, t, base);
- }
-
-
-
- template<typename T>
- static _Myt fromNumber ( T number, int base = 10 )
- {
- basic_stringstream<_Elem, _Traits, _Ax> ss;
- ss.setf(BaseFlag(base), ios_base::basefield);
- ss << number;
- return ss.str();
- }
-
-
-
-
-
- static _Myt fromNumber ( float number, _Elem f = _Elem(‘g‘), int prec = 6 )
- {
- return fromNumber(static_cast<double>(number), f, prec);
- }
-
-
- static _Myt fromNumber ( double number, _Elem f = _Elem(‘g‘), int prec = 6 )
- {
- basic_stringstream<_Elem, _Traits, _Ax> ss;
- ss << setprecision(prec);
- if ( _Traits::eq(f, _Elem(‘f‘)) )
- ss << setiosflags(ios_base::fixed);
- else if ( _Traits::eq(f, _Elem(‘e‘)) || _Traits::eq(f, _Elem(‘E‘)) )
- ss << setiosflags(ios_base::scientific);
- ss << number;
- return ss.str();
- }
- #pragma endregion //convert between numbers
-
-
- #pragma region "format string"
-
-
- void format(const _Elem* szFormat, ...)
- {
- if(!szFormat)
- return;
- va_list argList;
- va_start(argList, szFormat);
- formatV(szFormat, argList);
- va_end(argList);
- }
-
-
- void formatV(const _Elem* szFormat, va_list argList)
- {
- if(!szFormat)
- return;
- int nLength = _ExTraits::ct_vscprintf(szFormat, argList);
- if(nLength < 0)
- return;
- resize(nLength);
- _ExTraits::ct_vstprintf_s(_Myptr(), nLength + 1, szFormat, argList);
- va_end(argList);
- }
- #pragma endregion //format string
- };
-
- typedef basic_exstring<char, char_traits<char>,
- allocator<char>, ex_char_traits<char> > exstring;
-
- typedef basic_exstring<wchar_t, char_traits<wchar_t>,
- allocator<wchar_t>, ex_char_traits<wchar_t> > exwstring;
使用举例:
- #include <iostream>
- #include <tchar.h>
- #include "exstring.h"
-
- #ifdef _UNICODE
- typedef exwstring tstring;
- #define tcout wcout
- #else
- typedef exstring tstring;
- #define tcout cout
- #endif //_UNICODE
-
- int main(int argc, char* argv[])
- {
- tstring s(_T("\t Hello ExString\r\n"));
- tcout << _T("result of triming left:") << s.trimLeft(_T("\t ")) << endl;
- tcout << _T("result of triming right:") << s.trimRight(_T("\r\n")) << endl;
- tcout << _T("result of compare") << s.compareNoCase(_T("hello exstring")) << endl;
- tcout << _T("result of converting to upper:") << s.toUpper() << endl;
- tcout << _T("result of converting to lower:") << s.toLower() << endl;
-
- tcout << _T("the left 5 chars:") << s.left(5) << endl;
- tcout << _T("the right 8 chars:") << s.right(8) << endl;
- tcout << _T("result of appending:") << s.append(_T(",exstring is practical")) << endl;
- tcout << _T("result of replacing:") << s.replace(_T("exstring"), _T("Exstring")) << endl;
-
- s.format(_T("sizeof(%s) is %d(0x%x)"), _T("exstring"), sizeof(exstring), sizeof(exstring));
- tcout << _T("result of formating:") << s << endl;
-
- tcout << tstring(_T("0xFF")).toNumber<int>(16) << endl;
- tcout << tstring(_T("-1")).toNumber<unsigned __int64>() << endl;
- tcout << tstring(_T("12.3456789")).toNumber<float>() << endl;
-
- tcout << tstring::fromNumber(255) << endl;
- tcout << _T("0x") << tstring::fromNumber(__int64(-1), 16).toUpper() << endl;
- tcout << tstring::fromNumber(12.3456789, _T(‘f‘), 4) << endl;
- tcout << tstring::fromNumber(12.3456789, _T(‘E‘), 4) << endl;
-
- return 0;
- }
输出:

http://blog.csdn.net/passion_wu128/article/details/38354541
扩展C++ string类
原文:http://www.cnblogs.com/findumars/p/5006182.html