4

Is there any function in C++ which converts all data types (double, int, short, etc) to string?

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
sundowatch
  • 3,012
  • 3
  • 38
  • 66
  • While not an exact duplicate, the answers to this question provide the usual methods of converting built-in types to strings: http://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c – Tyler McHenry Jul 20 '10 at 18:13
  • [sprintf](http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/) – Kirill V. Lyadvinsky Jul 20 '10 at 18:13
  • 2
    @Kirill `sprintf` is C. There are better, type-safe alternatives in C++. – Tyler McHenry Jul 20 '10 at 18:17
  • Do you really want to convert types to strings (answered by me) or values of types to string (already answered by others)? Converting types to strings is explained here http://stackoverflow.com/questions/81870/print-variable-type-in-c . – Peter G. Jul 20 '10 at 18:20
  • @Tyler McHenry, Surely there are type-safe alternatives, buf if we are talking about one function... Alternatives are not so lightweight. Anyway this is just a comment for information, I didn't give it as an answer. – Kirill V. Lyadvinsky Jul 20 '10 at 18:20

7 Answers7

8

Usually you'll use the << operator, in conjunction with (for example) a std::stringstream.

TreDubZedd
  • 2,571
  • 1
  • 15
  • 20
3

If boost is not an option (it should always be, but just in case):

#include <sstream>
#include <string>

template<class T1, class T2>
T1 lexical_cast(const T2& value)
{
    std::stringstream stream;
    T1 retval;

    stream << value;
    stream >> retval;

    return retval;
}

template<class T>
std::string to_str(const T& value)
{
    return lexical_cast<std::string>(value);
}

Boost has a similar idea, but the implementation is much more efficient.

sukru
  • 2,229
  • 14
  • 15
2

There is no built-in universal function, but boost::lexical_cast<> will do this.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
1

Why do you need this conversion? A lot of languages have variant types which auto-convert, and this can lead to wanting that behavior in C++ even though there may be a more canonical way of implementing it.

For example if you're trying to do output, using a (string)stream of some sort is probably the way to go. If you really need to generate and manipulate a string, you can use boost::lexical_cast http://www.boost.org/doc/libs/1_43_0/libs/conversion/lexical_cast.htm.

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

Here is the one I use from my utility library. This was condensed from other posts here on stackoverflow, I am not claiming this as my own original code.

#include <string>
#include <sstream>

using namespace std;

template <class T>
string ToString(const T& Value) {
    stringstream ss;
    ss << Value;
    string s = ss.str();
    return s;
}

also, another handy string formatting utility I use:

#include <string>
#include <stdarg.h> /* we need va_list */

// Usage: string myString = FormatString("%s %d", "My Number =", num);
string FormatString(const char *fmt, ...) {

    string retStr;

    if (NULL != fmt) {
        va_list marker = NULL;
        va_start(marker, fmt);
        size_t len = 256 + 1; // hard size set to 256
        vector <char> buffer(len, '\0');
        if (vsnprintf(&buffer[0], buffer.size(), fmt, marker) > 0) {
            retStr = &buffer[0]; // Copy vector contents to the string
        }
        va_end(marker);
    }

    return retStr;
}
SuperJames
  • 767
  • 4
  • 14
0

For this use stringstream. First include the header file as #include . Then create an object of stringstream and using stream insertion operator (<<) pass the contents you want to convert as string. Ex:

#include <iostream>
#include <sstream>
int main(){
    std::string name = "Ram";
    float salary = 400.56;
    std::stringstream obj;
    obj << name << " salary: " << salary;
    std::string s = obj.str();
    std::cout << s;
}
Abhishek Rathore
  • 1,016
  • 1
  • 11
  • 13