C++ {fmt}只替换字符串中的一个位置参数和多个位置参数

C++ {fmt}只替换字符串中的一个位置参数和多个位置参数,c++,fmt,C++,Fmt,我想知道{fmt}库是否只允许修改包含多个位置参数的字符串中的一个位置参数 这是我的测试,它不起作用,{fmt}文档显示没有解决方案 std::string text = "{name} is {nb} years old and lives in {city}"; try { text = fmt::format(text, fmt::arg("name", "John")); } catch (const fmt::form

我想知道{fmt}库是否只允许修改包含多个位置参数的字符串中的一个位置参数

这是我的测试,它不起作用,{fmt}文档显示没有解决方案

std::string text = "{name} is {nb} years old and lives in {city}";
try
{
    text = fmt::format(text, fmt::arg("name", "John"));
}
catch (const fmt::format_error::exception &e)
{
    std::cout << e.what() << std::endl;
}
std::string text=“{name}已{nb}岁,居住在{city}”;
尝试
{
text=fmt::format(text,fmt::arg(“name”,“John”));
}
捕获(常量fmt::格式错误::异常&e)
{

std::coutNo。引用一个不存在的参数,不管是否是位置参数,都是一个错误。

如果
name
的值是
{city}
,那么结果将是
{city}是{nb}岁并且住在{city}
。当您稍后将此内容输入到
fmt
中,在
nb
city
中填写时,您会收到“垃圾”(即不可预测的结果),这就是不允许不完整替换的原因。请参阅@j6t的注释。您可以始终传递
{name}
{city}
用于
名称
城市
,如果它们不可用。