添加std::string的format函数
来源:互联网 发布:python 行为树 实现 编辑:程序博客网 时间:2024/06/06 01:50
From: http://blog.csdn.net/tingsking18/article/details/6699274
//============================================================================// Name : main.cpp// Author : Jing// Version :// Copyright : Jing// Description : Hello World in C++, Ansi-style//============================================================================#include <string>#include <stdio.h>#include <iostream>#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);}SString& operator=(const _CharT* __s){return this->assign(__s);}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->append(szBuffer);return *this;}};typedef SString<char, char_traits<char> , allocator<char> > CString;int main(int argc, char* argv[]){CString s("a");CString s1 = s.Format("abc%d", 1);CString s4 = s;CString s2(s1);CString s3(s1.begin(), s1.end());cout << s3.c_str() << endl;int i;cin >> i;return 0;}
0 0
- 添加std::string的format函数
- 实现std::string的format函数
- 给std::string增加format函数
- 给std::string增加format函数
- [C/C++标准库]_[初级]_[如何实现std::string自己的Format(sprintf)函数]
- std::string的工具函数
- std::string的工具函数
- std::string 的构造函数
- std::string的工具函数
- std::string的工具函数
- std::string的工具函数
- std::string的split函数
- std::string的工具函数
- std::string的工具函数
- std::string的工具函数
- 关于std::string与Format
- string.Format函数的用法
- string.format()函数的用法
- 给移动App创业者的工具集
- Fragment(3)和其他Fragment之间交互--伟大的接口回调
- Handler和Message(一)
- 脚本分享-两个shell备份mysql的脚本
- HTML5 LocalStorage 本地存储
- 添加std::string的format函数
- 拼json字符串输出java对象
- ACE OLEDB 12.0连接字符串(Excel 2007)
- 自己的APP里跳转到系统的日历
- 盘古
- openfire及其源码的开发和部署
- SpringMVC中实现自己的拦截器
- 关于android设备不自带sqlite3问题(sqlite3 not found)
- WCF----双工模式服务端回调客户端方法时无响应,“发送到 http://***/User.svc 的请求操作在配置的超时内未收到回复”