boost::format库的使用
来源:互联网 发布:有声阅读软件 编辑:程序博客网 时间:2024/05/23 22:48
库原型如下啊:
namespace boost {template<class charT, class Traits=std::char_traits<charT> > class basic_format {public: typedef std::basic_string<charT, Traits> string_t; typedef typename string_t::size_type size_type; basic_format(const charT* str); basic_format(const charT* str, const std::locale & loc); basic_format(const string_t& s); basic_format(const string_t& s, const std::locale & loc); basic_format& operator= (const basic_format& x); void clear(); // reset buffers basic_format& parse(const string_t&); // clears and parse a new format string string_t str() const; size_type size() const; // pass arguments through those operators : template<class T> basic_format& operator%(T& x); template<class T> basic_format& operator%(const T& x); // dump buffers to ostream : friend std::basic_ostream<charT, Traits>& operator<< <> ( std::basic_ostream<charT, Traits>& , basic_format& ); // Choosing which errors will throw exceptions : unsigned char exceptions() const; unsigned char exceptions(unsigned char newexcept);// ............ this is just an extract .......}; // basic_formattypedef basic_format<char > format;typedef basic_format<wchar_t > wformat;// free function for ease of use :template<class charT, class Traits> std::basic_string<charT,Traits> str(const basic_format<charT,Traits>& f) { return f.str();}} // namespace boost
下面是一个例子:
/** 2 @author Amiber 3 @date 2012-12-16 4 @brief boost::format 5 **/ 6 7 #include <iostream> //for std::cout 8 #include <exception> 9 10 #include <boost/format.hpp> //for format 11 #include <boost/static_assert.hpp> // for BOOST_ASSERT 12 13 int main(int argc,char* argv[]) 14 { 15 16 int a = 10; 17 int b = 11; 18 double c = 23.2344; 19 20 /** 21 format %N% %N% 22 **/ 23 std::cout<<boost::format("%1%,%2%,%1%") % a % b<<std::endl; 24 25 /** 26 format %d 27 **/ 28 29 std::cout<<boost::format("%d,%d") % a %b <<std::endl; 30 31 32 /** 33 format %1$ [flag] [...] 34 **/ 35 36 std::cout<<boost::format("%.3f") %c <<std::endl; 37 std::cout<<boost::format("%1$+5.3f") %c <<std::endl; 38 39 std::cout<<boost::format("%1$p") % a <<std::endl; 40 41 /** 42 format group() 43 **/ 44 45 std::string str="Amiber"; 46 47 std::cout<<boost::format("My name is %1%,my money is %2%") % str % boost::io::group(std::hex,1430)<<std::endl; 48 49 /** 50 boost::format::clear 51 boost::format::parse 52 **/ 53 54 std::string formstr="%d<->%d"; 55 boost::format fmt(formstr); 56 57 std::cout<<fmt % a % b <<std::endl; 58 59 fmt.clear(); 60 61 fmt.parse("%d>-<%d"); 62 63 64 std::cout<<fmt % a %b <<std::endl; 65 66 /** 67 boost::format::size 68 boost::format::str 69 **/ 70 71 BOOST_ASSERT(fmt.size() !=0); 72 73 std::cout<<fmt.str()<<std::endl; 74 75 /** 76 boost::io::too_many_args 77 boost::io::too_few_args 78 boost::io::bad_format_string 79 **/ 80 81 try 82 { 83 std::cout<<fmt % 1 % 2 %3 <<std::endl; 84 }catch(const boost::io::too_many_args& e) 85 { 86 std::cout<<"This exceptions is "<<e.what()<<std::endl; 87 } 88 89 90 try 91 { 92 std::cout<<fmt % 1 <<std::endl; 93 }catch(const boost::io::too_few_args& e) 94 { 95 std::cout<<"Another exception is "<<e.what()<<std::endl; 96 } 97 return 0; 98 }
boost::format库提供对了输出的个数类型进行格式化的需求,这里仅作为一个参考,要想更加学会format,建议去看boost教程
- boost::format库的使用
- boost 的 format 库
- boost中format的使用
- 使用Boost中的Format格式化标准模版库的string
- 使用Boost Format库实现sprintf和CString的Format功能
- boost库之format
- BOOST库之format
- (boost::format)io_service和io_service::work的基础使用
- boost::format(字符串格式化库)
- boost::format
- boost::format
- BOOST::Format
- boost::format
- boost::format
- boost库的使用
- Boost库的使用
- Boost 的 C++ 格式化輸出函式庫:Format
- Boost 的 C++ 格式化輸出函式庫:Format
- 利用Matlab设计滤波器(FDAT) .
- JSP与Servlet面试题精选
- 任务四指来指去
- C语言实现24点游戏
- c/c++ int _tmain(int argc, _TCHAR* argv[])
- boost::format库的使用
- ubuntu install NFS service
- C# 获取进程或线程的相关信息
- NPAPI插件开发详细记录:实用功能——添加、识别私有属性
- 注册表编程基础+实例
- ManualResetEvent详解
- One Piece,One Dream (海贼王与梦想--短篇演讲稿)
- KindEditor的使用和上传图片的后台处理
- unity自带寻路Navmesh入门教程(二)