给std::string增加format函数

From:  http://blog.csdn.net/tingsking18/article/details/6699274

  1. //============================================================================   
  2. // Name        : main.cpp   
  3. // Author      : Jing   
  4. // Version     :   
  5. // Copyright   : Jing   
  6. // Description : Hello World in C++, Ansi-style   
  7. //============================================================================   
  8.   
  9. #include <string>   
  10. #include <stdio.h>   
  11. #include <iostream>   
  12. #include <stdarg.h>   
  13. using namespace std;  
  14.   
  15. template<typename _CharT, typename _Traits, typename _Alloc>  
  16. class SString: public basic_string<_CharT, _Traits, _Alloc>  
  17. {  
  18. public:  
  19.     typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;  
  20.   
  21.     typedef typename _CharT_alloc_type::size_type size_type;  
  22.   
  23.     static const size_type npos = static_cast<size_type> (-1);  
  24.   
  25.   
  26.     inline SString() :  
  27.         basic_string<_CharT, _Traits, _Alloc> ()  
  28.     {  
  29.     }  
  30.     explicit SString(const _Alloc& __a) :  
  31.         basic_string<_CharT, _Traits, _Alloc> ()  
  32.     {  
  33.     }  
  34.   
  35.     SString(const SString& __str) :  
  36.         basic_string<_CharT, _Traits, _Alloc> (__str)  
  37.     {  
  38.     }  
  39.   
  40.     SString(const SString& __str, size_type __pos, size_type __n = npos) :  
  41.         basic_string<_CharT, _Traits, _Alloc> (__str, __pos, npos)  
  42.     {  
  43.     }  
  44.   
  45.     SString(const SString& __str, size_type __pos, size_type __n,  
  46.             const _Alloc& __a) :  
  47.         basic_string<_CharT, _Traits, _Alloc> (__str, __pos, __n, __a)  
  48.     {  
  49.     }  
  50.   
  51.     SString(const _CharT* __s, size_type __n, const _Alloc& __a = _Alloc()) :  
  52.         basic_string<_CharT, _Traits, _Alloc> (__s, __n, __a)  
  53.     {  
  54.     }  
  55.   
  56.     SString(const _CharT* __s, const _Alloc& __a = _Alloc()) :  
  57.         basic_string<_CharT, _Traits, _Alloc> (__s, __a)  
  58.     {  
  59.     }  
  60.   
  61.     SString(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) :  
  62.         basic_string<_CharT, _Traits, _Alloc> (__n, __c, __a)  
  63.     {  
  64.     }  
  65.   
  66.     template<class _InputIterator>  
  67.     SString(_InputIterator __beg, _InputIterator __end, const _Alloc& __a =  
  68.             _Alloc()) :  
  69.         basic_string<_CharT, _Traits, _Alloc> (__beg, __end, __a)  
  70.     {  
  71.     }  
  72.   
  73.     SString& operator=(const SString& __str)  
  74.     {  
  75.         return this->assign(__str);  
  76.     }  
  77.   
  78.     SString& operator=(const _CharT* __s)  
  79.     {  
  80.         return this->assign(__s);  
  81.     }  
  82.   
  83.     SString& operator=(_CharT __c)  
  84.     {  
  85.         this->assign(1, __c);  
  86.         return *this;  
  87.     }  
  88.   
  89.     inline SString &Format(const char *_format, ...)  
  90.     {  
  91.         char szBuffer[1000];  
  92.         memset(szBuffer, 0x00, sizeof(szBuffer));  
  93.   
  94.         va_list ap;  
  95.         va_start(ap, _format);  
  96.   
  97.         try  
  98.         {  
  99.             vsnprintf(szBuffer, 1000, _format, ap);  
  100.         } catch (...)  
  101.         {  
  102.             cout << "ERROR: format the string failed..." << endl;  
  103.             return *this;  
  104.         }  
  105.   
  106.         va_end(ap);  
  107.         this->append(szBuffer);  
  108.         return *this;  
  109.     }  
  110.   
  111. };  
  112. typedef SString<char, char_traits<char> , allocator<char> > CString;  
  113.   
  114. int main(int argc, char* argv[])  
  115. {  
  116.   
  117.     CString s("a");  
  118.     CString s1 = s.Format("abc%d", 1);  
  119.     CString s4 = s;  
  120.     CString s2(s1);  
  121.     CString s3(s1.begin(), s1.end());  
  122.     cout << s3.c_str() << endl;  
  123.     int i;  
  124.     cin >> i;  
  125.     return 0;  
  126.   
  127. }  

Guess you like

Origin blog.csdn.net/JoeBlackzqq/article/details/42265673