boost::format库的使用

库原型如下啊:

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_format

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值