c 语言format函数,写c函数format_string用于格式化std :: string的sprintf

One option is have temp buffer start some known size then increase it if see it’s not enough with vsnprintf. Are there better approach? Thanks

您可以使用vasprintf(),但这会进行不必要的堆分配 – 平均来说不太可能更快.使用alloca可以避免堆.或者,您可以直接写入返回的字符串:NRVO应该避免复制,并且从C 11开始,移动语义会将成本sans-NRVO限制为几个指针交换.

#include

#include

#include

#include

#include

std::string stringf(const char* format, ...)

{

va_list arg_list;

va_start(arg_list, format);

// SUSv2 version doesn't work for buf NULL/size 0, so try printing

// into a small buffer that avoids the double-rendering and alloca path too...

char short_buf[256];

const size_t needed = vsnprintf(short_buf, sizeof short_buf,

format, arg_list) + 1;

if (needed <= sizeof short_buf)

return short_buf;

// need more space...

// OPTION 1

std::string result(needed, ' ');

vsnprintf(result.data(), needed, format, arg_list);

return result; // RVO ensures this is cheap

OR

// OPTION 2

char* p = static_cast(alloca(needed)); // on stack

vsnprintf(p, needed, format, arg_list);

return p; // text copied into returned string

}

int main()

{

std::string s = stringf("test '%s', n %8.2f\n", "hello world", 3.14);

std::cout << s;

}

一个更简单,最初更快的选择是:

std::string result(255, ' '); // 255 spaces + NUL

const size_t needed = vsnprintf(result.data(), result.size() + 1,

format, arg_list);

result.resize(needed); // may truncate, leave or extend...

if (needed > 255) // needed doesn't count NUL

vsnprintf(result.data(), needed + 1, format, arg_list);

return result;

潜在的问题是你分配了至少256个字符,但是存储的实际文本很短:这可能会增加你的内存/缓存相关性能.您可以使用[shrink_to_fit] http://en.cppreference.com/w/cpp/string/basic_string/shrink_to_fit)解决该问题,但标准并不要求它实际执行任何操作(要求是“非绑定”).如果您最终必须复制到一个新的精确大小的字符串,您可能也使用了本地char数组.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值