探究C++中string类的实现原理以及扩展使用

2020-01-06 14:08:06王旭

使用举例:


#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 ExStringrn")); 
  tcout << _T("result of triming left:") << s.trimLeft(_T("t ")) << endl; 
  tcout << _T("result of triming right:") << s.trimRight(_T("rn")) << 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; 
} 

输出:

探究C++中string类的实现原理以及扩展使用



注:相关教程知识阅读请移步到C++教程频道。