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教程


原创粉丝点击