caracters and variables concatenation

John Love-Jensen eljay@adobe.com
Mon Jan 20 13:16:00 GMT 2003


Hi Search Info,

>I want to get rid of the sprintf function and do the same functionality with
pure C++.

Use std::ostringstream, located in <sstream>.

Using sprintf:
string foo(const char* str, int i) {
    char buffer[1024];
    sprintf(buffer, "%s%d", str, i);
    return string(buffer);
}

Using "pure C++" ostringstream:
string foo(const char* str, int i) {
    ostringstream o;
    o << str << i;
    return o.str();
}

NOTE: you could use...
string foo(const string& str, int i) {
...instead.

--Eljay



More information about the Gcc-help mailing list