std::string stringf(const char* format, ...)
2015-01-19 09:48
465 查看
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...
char* p = static_cast<char*>(alloca(needed));
vsnprintf(p, needed, format, arg_list);
return p;
}
{
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...
char* p = static_cast<char*>(alloca(needed));
vsnprintf(p, needed, format, arg_list);
return p;
}
相关文章推荐
- convert a std::string to const char* or char*
- VS2010不能将参数 1 从“std::string”转换为“const char *”解决
- VS2010不能将参数 1 从“std::string”转换为“const char *”解决
- C++ 不能通过‘...’传递有不能平凡复制的类型‘const string {aka const class std::basic_string<char>}’
- ANSI与UTF-8编码互相转换,CString,std::string,char*,const char*互相转换
- "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"
- const char * 和 std::string.c_str()是个危险的东西!
- 二进制“<<”: 没有找到接受“const std::basic_string<char,std::char_traits<char>,std::allocator<char>>”类型的右操作数的运算
- C++编译出现binding ‘const string {aka const std::__cxx11::basic_string<char>}’ to reference of type ‘std
- VS2010不能将参数 1 从“std::string”转换为“const char *”解决
- cannot convert parameter 1 from 'std::string' to 'const char *'
- 为什么std::string不提供到const char *的operator?
- [C++]char* const char* std::string区分
- "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"问题的解决办法
- 如何解决error C2664: “atof”: 不能将参数 1 从“std::string”转换为“const char *”
- Convert std::string to const char* or char*
- cocos2d-x 关于 std::string 与 const char* 之间转换的奇怪问题
- NSString,NSData,std::string,const char*相互转化
- std::string ,QString和const char * 的互相转换
- VS2010不能将参数 1 从“std::string”转换为“const char *”解决