場景:html
1. C語言有本身的sprintf函數,可是這個函數有個缺點,就是不知道須要建立多大的buffer, 這時候能夠使用snprintf函數來計算大小,只要參數 buffer爲NULL, count爲0便可.函數
2. 這裏實現std::string本身的sprintf也是用了snprintf的特性,先計算大小,再建立空間,以後存入std::string.ui
3. 還使用了C的可變參數特性..net
[cpp] view plaincopyprint?設計
- std::wstring Format(const wchar_t *format,...)
- {
- va_list argptr;
- va_start(argptr, format);
- int count = _vsnwprintf(NULL,0,format,argptr);
- va_end(argptr);
-
- va_start(argptr, format);
- wchar_t* buf = (wchar_t*)malloc(count*sizeof(wchar_t));
- _vsnwprintf(buf,count,format,argptr);
- va_end(argptr);
-
- std::wstring str(buf,count);
- free(buf);
- return str;
- }
讓咱們看看可變參數的聲明:orm
[cpp] view plaincopyprint?htm
- typedef char * va_list;
[cpp] view plaincopyprint?blog
- #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
-
- #define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
- #define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
- #define _crt_va_end(ap) ( ap = (va_list)0 )
注意: ap會累加,每次調用va_arg都會指向下一個參數,問題就是va_arg並不知道何時結束,因此若是設計其餘的可變參數的函數,要先傳入一個參數個數做爲方法參數.get
snprintf 源碼實現是經過計算%的個數來判斷參數個數的.源碼
參考:
http://blog.csdn.net/echoisland/article/details/6086406
https://msdn.microsoft.com/en-us/library/1kt27hek.aspx
https://msdn.microsoft.com/en-us/library/2ts7cx93.aspx
[plain] view plaincopyprint?
- If buffer is a null pointer and count is zero, len is returned as the count of characters required to format the output, not including the terminating null.
- To make a successful call with the same argument and locale parameters, allocate a buffer holding at least len + 1 characters.