一个格式化字符串的函数ToString

  A Formatting String Function  原文:http://flounder.com/tostring.htm

CString ToString(LPCTSTR fmt, ...);
CString ToString(UINT fmtid, ...);
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
#include "stdafx.h" 
#include "ToString.h" 
/**************************************************************************** 
*                         ToString 
* Inputs: 
*     LPCTSTR fmt: Format code 
*     ...: Values to format 
* Result: CString 
*     The values formatted to a string according to the format string 
****************************************************************************/
 

CString ToString(LPCTSTR fmt, ...) 

    va_list args; 
    va_start(args,fmt); 

    CString s; 
    s.FormatV(fmt, args); 

    va_end(args); 
    
return s; 
// ToString 
   
/**************************************************************************** 
*                         ToString 
* Inputs: 
*     UINT fmt: String ID of formatting string 
*     ...: parameters to formatting string 
* Result: CString 
*     The result of the formatting 
****************************************************************************/
 

CString ToString(UINT fmtid, ...) 

    va_list args; 
    va_start(args,fmtid); 

    CString fmt; 
    fmt.LoadString(fmtid); 

    CString s; 
    s.FormatV(fmt, args); 

    va_end(args); 
    
return s; 
// ToString   

  使用:
  CString s = ToString(_T("value = %d"), value);
  SomeFunction(ToString(_T("(%d, %d)"), x, y);

posted on 2017-06-15 15:53  我来乔23  阅读(294)  评论(0编辑  收藏  举报

导航