C ++ 11相当于Boost.Format

时间:2014-05-01 17:26:38

标签: c++11 boost boost-format

像C ++ 11标准中的Boost.Format一样吗?我已经能够避免将Boost与更好的C ++ 11选项一起用于我所拥有的其他所有需求。

就此而言,Boost.Format并没有对Python format()的语法有所了解。这样的事情会更好。

4 个答案:

答案 0 :(得分:15)

正如nosid正确指出的那样,C ++ 11和C ++ 14都没有提供与Boost格式相同的功能。

但是,fmt library可选地使用C ++ 11功能(如可变参数模板)提供了类似Python的format函数的实现:

std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy");

以及*printf函数的安全替代方法:

fmt::printf("The answer is %d\n", 42);

免责声明:我是这个图书馆的作者

答案 1 :(得分:8)

有一项与 boost-format 类似的提案。但是,它既不是C ++ 11也不是C ++ 14的一部分,也不会添加任何与字符串格式相关的内容。

您可以在这里找到最新的提案。与 boost-format 相反,它基于可变参数模板。

答案 2 :(得分:1)

使用c ++ 11正则表达式和可变参数模板的类似Python的格式字符串函数实现。

/**
   Helper code to unpack variadic arguments
*/
namespace internal
{
    template<typename T>
    void unpack(std::vector<std::string> &vbuf, T t)
    {
        std::stringstream buf;
        buf << t;
        vbuf.push_back(buf.str());
    }
    template<typename T, typename ...Args>
    void unpack(std::vector<std::string> &vbuf, T t, Args &&... args)
    {
        std::stringstream buf;
        buf << t;
        vbuf.push_back(buf.str());
        unpack(vbuf, std::forward<Args>(args)...);
    }
}

/**
    Python-like string formatting
 */
template<typename ... Args>
std::string format(const std::string& fmt, Args ... args)
{
    std::vector<std::string> vbuf;  // store arguments as strings
    std::string in(fmt), out;    // unformatted and formatted strings
    std::regex re_arg("\\{\\b\\d+\\b\\}");  // search for {0}, {1}, ...
    std::regex re_idx("\\b\\d+\\b");        // search for 0, 1, ...
    std::smatch m_arg, m_idx;               // store matches
    size_t idx = 0;                         // index of argument inside {...}

    // Unpack arguments and store them in vbuf
    internal::unpack(vbuf, std::forward<Args>(args)...);

    // Replace all {x} with vbuf[x]
    while (std::regex_search(in, m_arg, re_arg)) {
        out += m_arg.prefix();
        if (std::regex_search(m_arg[0].str(), m_idx, re_idx)) {
            idx = std::stoi(m_idx[0].str());
        }
        if(idx < vbuf.size()) {
            out += std::regex_replace(m_arg[0].str(), re_arg, vbuf[idx]);
        }
        in = m_arg.suffix();
    }
    out += in;
    return out;
}

示例:cpp.sh/6nli

答案 3 :(得分:0)

使用sprintf

  • 对于C ++ 20,请使用std::format
  • 在C ++ 11版本中有fmt-library
  • 格式化输出的一些简单解决方案是printf
  • 要使用printf语法编写std::string,请使用以下代码段

最小可重现的示例:采用printf语法的格式std::string

interactive version

#include <iostream>
#include <string>
#include <stdio.h>
#include <assert.h>


template<typename... Args>
std::string fmt_str(std::string fmt, Args... args)
{
    size_t bufferSize = 1000;
    char *buffer = new char[bufferSize];
    int n = sprintf(buffer, fmt.c_str(), args...);
    assert (n >= 0 and n < (int) bufferSize - 1  && "check fmt_str output");

    std::string fmtStr (buffer);
    delete buffer;
    return fmtStr;
}



int main()
{
    int a=1, b=2;
    double c=3.;
    std::cout << fmt_str("%d plus %d is %f", a, b, c) << std::endl;
    return 0;
}

输出

1 plus 2 is 3.000000