Click here to Skip to main content
13,345,438 members (49,573 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 27 Feb 2008

STL Format

, 27 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Put your favourite format string and function arguments to STL string.


Many nowadays applications with user interface are using string manipulation routines for sure. It is a programmer’s right to choose the best and most convenience tool for a particular situation. I guess that the oldest one should be standard library’s function called “printf” and its derivations. Why yet another printf like function? Because, it is fun and should be useful for particular situations. Interested? Read bellow…


You can choose whatever you want to format your string for particular situation. My best guess that you already know at least one that is right for you. I can write a short list that I know and am using for daily work: STL (streams), MFC/WTL (CString), CRT/libc(printf), boost::format, there are few articles like this in too. Now, I will add my format function to this list.

I don’t like streams because if you need to add something to it with particular format, you need to call many additional functions like “width, setf” and so on. It is not very convenience if you have large text to stream, many formats to change. So, the alternative for this would be “CString” class and/or “printf” functions, because you “call” all these functions with format string and argument list. “CString” class is quite good, but it is very specific, so if you write programs for many platforms, you might not be using this. Function “printf” is good, but you have to know how many symbols it will pop-out, you will not have dynamic buffer here. Are there any alternatives? Library boost, but I did not like its speed. I will confess, I didn’t test it, but I saw results on the net, I didn’t like them. What are other alternatives? Write your own and be happy with it.

The beginning

Safety is #1 at programming world. Well, you will not get this with “printf” and with my format function. They both use the same “stdarg” mechanism which is good if you know what you are doing. But if you want to compromise application, you can easily change format string and/or argument order. You should consider that with safety you loose speed and choose the right method for you.

So, let’s look closer, what kind of reinvented wheel do we have? I have read much about string formats and made decision: my format library will be very close with printf “standards”. Why? Because it is very convenience and it is already in my blood, and it is very hard to convince other programmers to use your library with new format. It is not wise. So, here it goes: old format with some weird enhancements:


[flags] optional flag.
'-': left alignment (default is right alignment)
'=': centered alignment
'+': show sign
'!': don't print int that is *0* or string that is *null*
'L': use long instead of int
'LL': use long long instead of long
'0': pad with pad char (if pad char is not specified, then fill with 0's)
'#': show base. o- 0, x- 0x, X- 0X

[:padchar] optional flag. Default value depends on [flags].

[width] optional flag. Specifies a minimal width for the string resulting form the conversion.

[.precision] optional flag. When outputting a floatting type number, it sets the maximum number of digits. When used with type-char s or S the conversion string is truncated to the precision first chars.

'b': binary output
'o': octal output
'i', 'u', 'd': decimal output (signed, unsigned)
'x': hexadecimal output ('X' upper case)
'f': fixed float format
's', 'S': string output
'c', 'C': char output
'%': print '%'

I guess if you use printf like formatting string, you will find familiar things in the format above. The new thing is “padchar”. I didn’t find feature like this (except STL streams), so I silently added this.

Using the code

To use my function, you need to include vformat.h file. The main function and all structs goes under "frmt" namespace. It is your right to add line "using namespace frmt;" or call function with "frmt::" prefix. To make things easier and more WindowsAPI alike you can make additional functions:

size_t formatA (string& sOut, const char *cpcFormat, ... )
    va_list vargs;
    va_start (vargs, cpcFormat);
    size_t iRes = vformat<char> (sOut, cpcFormat, vargs);
    va_end (vargs);
    return iRes;
size_t formatW (wstring& sOut, const wchar_t *cpwcFormat, ... )
    va_list vargs;
    va_start (vargs, cpwcFormat);
    size_t iRes = vformat<wchar_t> (sOut, cpwcFormat, vargs);
    va_end (vargs);
    return iRes;

It is wise to check "iRes" variable in debug versions, because it shows where error in format string lies (0- on success).

Points of Interest

I bet you want to see how fast my format function is? Well, I have to disappoint you: it is not as fast as I or you wanted, but it is promising. In some cases it is very similar with CRT/libc printf speed, but in most cases it is a bit slower. Look at the speed table and you will understand (D stands for debug, R for release) (the time is in ms):

<td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ece9d8; WIDTH: 55.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent" valign=removed" width="74">


Format string


printf (D)


printf (R)






%-#4x %+#012x %+#012o %#8x




















%0#-+3.5s %0#-+5.3s %0#-+s




Well, numbers are numbers, they speak for their selves. Debug version is slow for sure, I cannot do anything with that, but release version is promising. As complexity of format string grows, my function’s time is very close to printf time. Also, please note very interesting thing: when format string grows, the time of printf function grows much faster than mine function’s. That is very interesting and funny results.

Final words

This function is small experience in my life of reinventing wheel. Will I use it? Yes! I wanted to write something similar with STL string, because I missed that feature in STL very much. Finally, I have done this and more over, I put this on the internet for mass judgment. Please, write me a short note if you start using my function and are happy of it. You are free to use it in any product, but don’t claim, that you wrote it.


  • A small bug-fix in string format: 'c' and 'C'.
  • Added additional ifdef's to make code compile under linux systems.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Romualdas Cukuras
Software Developer (Senior)
Lithuania Lithuania
It is hard to understand me in real life. My ideas are strong, but requires many brain power to understand. But once you get it - you will never forget.

You may also be interested in...


Comments and Discussions

Bugpossible bug when format floating number Pin
wuhao_xianyang13-May-14 18:34
memberwuhao_xianyang13-May-14 18:34 
Generalstringstream is my choice Pin
zengkun1001-Apr-08 4:21
memberzengkun1001-Apr-08 4:21 
GeneralNot so great for intenationalization Pin
Mihai Nita27-Mar-08 12:28
memberMihai Nita27-Mar-08 12:28 
Generalpossible bug wchar_t version Pin
Albertino27-Mar-08 2:18
memberAlbertino27-Mar-08 2:18 
GeneralRe: possible bug wchar_t version [modified] Pin
Romualdas Cukuras27-Mar-08 2:30
memberRomualdas Cukuras27-Mar-08 2:30 
GeneralBOOST.Format Pin
Serdat Nerkim27-Feb-08 12:08
memberSerdat Nerkim27-Feb-08 12:08 
GeneralRe: BOOST.Format Pin
Stephen Hewitt27-Feb-08 16:23
mvpStephen Hewitt27-Feb-08 16:23 
GeneralRe: BOOST.Format Pin
Romualdas Cukuras27-Feb-08 21:15
memberRomualdas Cukuras27-Feb-08 21:15 
GeneralRe: BOOST.Format Pin
Warren Stevens27-Mar-08 3:14
memberWarren Stevens27-Mar-08 3:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 27 Mar 2008
Article Copyright 2008 by Romualdas Cukuras
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid