zoukankan      html  css  js  c++  java
  • boost format

    Boost.Format provides a class called boost::format, wich is defined in boost/format.hpp Similar to std::printf(), a string containing special characters to control formatting is passed to the constructor of boost::format. The data that replaces these special characters in the output is linked via the operator operator%

    #include <boost/format.hpp>
    #include <iostream>
    
    int main() {
      std::cout << boost::format(%1%.%2%.%3%) % 12 % 5 % 2014 << std::endl;
    std::cout << boost::format(%2%/%1%/%3%) % 12 % 5 % 2014 << std::endl;
    return 0; }

    The boost format string uses numbers placed between two percent signs as placeholders for the actual data, which will be linked in using operator%.

    输出为:

    12.5.2014

    5/12/2014

    Boost.Format is boty type safe and extensible. Objects of any type can be used with Boost.Format as long as the operator operator<< is overloaded for std::ostream.

    #include <boost/format.hpp>
    #include <string>
    #include <iostream>
    
    struct animal {
      std::string name;
      int legs;
    };
    
    std::ostream& operator<<(std::ostream& os, const animal& a) {
      return os << a.name << "," << a.legs;
    }
    
    int main() {
      animal a{"cat", 4};
      std::cout << boost::format("%1%") %a << std::endl;
      return 0;
    }

    输出为:cat,4

    uses boost::format to write an object of the user-defined type animal to standard output. This is possible because the stream operator is overloaded for animal.

  • 相关阅读:
    MySql——编程
    MySql——事务控制语言(DTL)
    wamp-php 集成环境的基础配置
    用户的 添加 权限 MySql远程登录
    使用模拟器调试移动端
    new关键字对构造函数做了什么
    H5与Native交互之JSBridge技术
    拍照上传头像图像旋转的问题
    Vue实现用户自定义上传头像裁剪
    H5图片压缩上传
  • 原文地址:https://www.cnblogs.com/sssblog/p/10978812.html
Copyright © 2011-2022 走看看