Fork me on GitHub

实现CString的Format功能,支持跨平台

#include <string>
#include <stdio.h>
#include <stdarg.h>

std::string& std_string_format(std::string & _str, const char * _Format, ...)
{
std::string tmp;

va_list marker;

va_list backup;
va_start(marker, _Format);

va_copy(backup,marker);

size_t num_of_chars = vsnprintf(0, 0, _Format, backup);
if (num_of_chars >= tmp.capacity()) {
tmp.resize(num_of_chars + 1);
}

vsnprintf((char *)tmp.data(), tmp.capacity(), _Format, marker);

va_end(marker);

va_end(backup);
_str = tmp.c_str();
return _str;
}

 

posted @ 2018-04-03 15:52  烟波--钓徒  阅读(338)  评论(0编辑  收藏  举报