像C++ 11标准中的Boost.Format一样吗?我已经能够避免使用Boost和更好的C++ 11选项来满足我所有的其他需求.
就此而言,Boost.Format对Python的语法没有任何影响format().这样的事情会更好.
我的命名空间ns中有一个函数可以帮助我打印STL容器.例如:
template <typename T>
std::ostream& operator<<(std::ostream& stream, const std::set<T>& set)
{
stream << "{";
bool first = true;
for (const T& item : set)
{
if (!first)
stream << ", ";
else
first = false;
stream << item;
}
stream << "}";
return stream;
}
Run Code Online (Sandbox Code Playgroud)
这适用于operator <<直接打印:
std::set<std::string> x = { "1", "2", "3", "4" };
std::cout << x << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是,使用boost::format是不可能的:
std::set<std::string> x = { "1", "2", "3", "4" };
boost::format("%1%") % x; …Run Code Online (Sandbox Code Playgroud) 以下是不可能的:
std::string s = boost::format("%d") % 1; // error
Run Code Online (Sandbox Code Playgroud)
你必须明确地调用str()方法:
std::string s = (boost::format("%d") % 1).str(); // OK
Run Code Online (Sandbox Code Playgroud)
它只是语法糖,但为什么不加入转换呢?
我想将浮点值格式化为n位有效数字,但从不使用科学记数法(即使它更短).
格式规范%f不涉及有效数字,%g有时会给我科学记数法(这对我来说不合适).
我想要表格中的价值观"123", "12.3", "1.23" or "0.000000123".
有没有一种优雅的方法来使用C++或boost来做到这一点?
假设我有一个类似printf函数(用于记录)利用完美转发:
template<typename... Arguments>
void awesome_printf(std::string const& fmt, Arguments&&... args)
{
boost::format f(fmt);
f % /* How to specify `args` here? */;
BlackBoxLogFunction(boost::str(f).c_str());
}
Run Code Online (Sandbox Code Playgroud)
(我没有编译这个,但我的实际功能遵循这个指导原则)
如何将variadic参数"展开"到boost :: format变量中f?
我想知道Boost.Format是否支持使用固定宽度/预分配缓冲区作为输出而不是lib本身管理的动态缓冲区?
也就是说,通常你会这样做:
boost::format myfmt("arg1: %1% / arg2: %2%");
// e.g.:
cout << (myfmt % 3.14 % 42);
// or
string s = boost::str( myfmt % "hey!" % "there!");
Run Code Online (Sandbox Code Playgroud)
因此Boost:Format lib将自动负责分配足够的空间并为您管理"输出缓冲区".
我想知道是否有任何方法可以使用具有Boost.Format的预定义非动态缓冲区,即:
const size_t buf_sz = 512;
char big_enough[buf_sz];
boost::format myfmt("arg1: %1% / arg2: %2%");
myfmt.attach_buffer(big_enough, buf_sz);
myfmt % "hey!" % "there!"
// big_enough buffer now contains the result string
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过示例,文档和来源进行筛选,但除了缺乏时间.(以及遗漏某些东西的可能性),有趣的是要知道: 如果不可能的话,如果有人可以解释原因(如果有特定的原因)会很好 - 这是故意的吗?它不符合API吗?...?
免责声明: 这个问题与性能无关!
我知道使用%s格式说明符和std::string这样会导致未定义的行为:
std::string myString = "test";
printf("%s", myString);
Run Code Online (Sandbox Code Playgroud)
但是,它保存到使用相同的符和std::string使用boost::format?
#include <boost/format.hpp>
int main()
{
std::string myString = "test";
boost::format fmt("%s");
fmt % myString;
std::cout << fmt.str();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
%s指定一个(const)char*,但我提供了一个std::string.这会导致UB吗?
它的目标之一是提供 printf 的替代品,这意味着 format 可以解析为 printf 设计的格式字符串,将其应用于给定的参数,并产生与 printf 相同的结果。
当我使用相同的格式字符串比较 boost:format 和 printf 的输出时,我得到了不同的输出。在线示例在这里
#include <iostream>
#include <boost/format.hpp>
int main()
{
boost::format f("BoostFormat:%d:%X:%c:%d");
unsigned char cr =65; //'A'
int cr2i = int(cr);
f % cr % cr % cr % cr2i;
std::cout << f << std::endl;
printf("Printf:%d:%X:%c:%d",cr,cr,cr,cr2i);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
升压格式: A:A:A:65
打印输出: 65:41:A:65
不同之处在于当我想将字符显示为整数类型时。
为什么有区别?这是一个错误还是想要的行为?
出于某种原因,我无法使用boost::format的boost::lambda.这是(希望)可编译的代码简化:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <boost/assign/list_of.hpp>
#include <boost/format.hpp>
#include <boost/lambda/lambda.hpp>
namespace bl = boost::lambda;
int main()
{
const std::vector<int> v = boost::assign::list_of(1)(2)(3);
std::for_each(v.begin(), v.end(), bl::var(std::cout) << std::setw(10) << bl::_1);
std::for_each(v.begin(), v.end(), bl::var(std::cout) << boost::format("%10d") % bl::_1);
}
Run Code Online (Sandbox Code Playgroud)
std::for_each产生预期的输出std::for_each只输出没有任何数字的空格这是为什么 ?我真的不熟悉boost::lambda所以我可能会错过这里明显的.
请不要建议std::copy基于答案:我的实际代码不起作用,std::vector但实际上是boost::fusion::vector(并且std::for_each实际上是a boost::fusion::for_each).
如何使用boost :: format将布尔值打印为符号值?
可以不这样做boost::io::group吗?似乎预先发送到流的标志会被重新测试:
#include <iomanip>
#include <iostream>
#include <boost/format.hpp>
int main()
{
std::cout
<< std::boolalpha
<< true << " "
<< boost::format("%1% %2%\n")
% true
% boost::io::group(std::boolalpha, true)
;
}
Run Code Online (Sandbox Code Playgroud)
输出量
true 1 true
Run Code Online (Sandbox Code Playgroud) 我正在尝试格式化二进制数组:char* memblock到十六进制字符串.
当我使用以下内容时:
fprintf(out, "0x%02x,", memblock[0]);
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
0x7f,
Run Code Online (Sandbox Code Playgroud)
当我尝试在这样的流上使用boost::format时:
std::ofstream outFile (path, std::ios::out); //also tried with binary
outFile << (boost::format("0x%02x")%memblock[0]);
Run Code Online (Sandbox Code Playgroud)
我得到这样一个怪异的输出(在六所示): 0x0^?.
是什么赋予了?
boost-format ×11
c++ ×10
boost ×9
c++11 ×2
format ×2
boost-lambda ×1
buffer ×1
c ×1
hex ×1
namespaces ×1
printf ×1