Sunday, August 2, 2009

Is there a text formatting function in C++?

Something like


format("Integer %i \n String %s",integer,string);


???

Is there a text formatting function in C++?
No, not in standard C++. As you indicated, printf() is still available to C++ as a library function. However, you stated that you're not interested in printf().





cout/ofstream and manipulators are considered far superior to the C standard library in most respects. Internally, the C++ implementation of stream I/O is much more efficient and the syntax can be considered more natural to code in many situations. For instance, cout statements don't need to be parsed at runtime unlike a printf() format string.





Also, printf() is not object-oriented so it is still around for backward compatibility but is not the preferred C++ output mechanism. With C++ stream I/O, you can actually extend %26lt;%26lt; with operator overloading to support custom output of your own classes/structures.

jasmine

No comments:

Post a Comment