C++: ostringstream 1

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page C++ : ostringstream 1 et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%

C++: building formatted strings more easily with ostringstream

In our section regarding Tricks and tips, we have presented the Logger which can be used to keep an execution log. This class works well, but one important element is missing: formatting the messages. See this example for the syslog function (a tool for logging system messages on Unix):

void syslog(int priority, const char* format, .../*arguments*/);

You can call this function with a variable number of parameters, like printf and its variants. The resulting message is built using a formatting string " %... ":

syslog(LOG_ERR, "ldap_open(%s %u) failed", MY_LDAP_SERVER, LDAP_PORT);

The Logger class does not give you this flexibility. The character string that is given is written literally to the log, without the possibility of formatting. However it is possible to do even better than syslog by using the ostringstream class. The StringStream class, defined below, derived from ostringstream, gives us all flexibility we need. By overloading the insertion (<<) and conversion (string()) operations, we can build a string that is as arbitrarily flexible and convert it to a string object in one sole expression.

File : stringstream.cpp
#include <sstream>
using namespace std;
class StringStream : public ostringstream
    // -----------------------------------------------------------------
    // Overloaded insertion operators to replace those defined in
    // ostream. We want << to return StringStream& instead of ostream&.
    // -----------------------------------------------------------------
    // insertion of standard and user defined data-types
    template <class T>
    StringStream& operator << (T item);
    // insertion of stream manipulators (cannot be instantiated
    // with template above)
    StringStream& operator << (ostream& (*manipulator)(ostream&));
    // -----------------------------------------------------------
    // Overloaded casting operator for string.
    // -----------------------------------------------------------
    operator string();
// --------------------------------------
// operator implementations
// --------------------------------------
template <class T>
StringStream& StringStream::operator <<  (T item)
    *(ostringstream*)this << item;
    return *this;
StringStream& StringStream::operator << (ostream& (*manipulator)(ostream&))
    *(ostringstream*)this << manipulator;
    return *this;
StringStream::operator string()
    return str();

This snippet shows how StringStream can be relevant in a call to LOGGER_WRITE:

             StringStream() << endl << "---------------------------"
                            << endl << "pi = "
                                    << setw(20) << right << scientific << M_PI
                            << endl << Sphere()
                            << endl << "---------------------------")
Outils personnels
Espaces de noms

Ressources de Calcul Québec