0

I wonder how it is possible to have java toString method in C++ in order to convert datatypes into the string. For example, with what way it would be possible to have following code in C++.

public byte[] myMsg = new byte[Length];
public int intValue;
public double doubleValue;
String out1 = new String();
String out2 = new String();
String out3 = new String();

out1 += ("X; " + Long.toString((intValue& 0x1fffffff) + 0x100000000L, 16).substring(1).toUpperCase() + ";");

out2 += (" " + (Integer.toString((this.myMsg[i] & 0xff) + 0x100, 16).substring(1)).toUpperCase());

out3 += ("; " + Double.toString(doubleValue));
utvecklare
  • 671
  • 1
  • 11
  • 25

4 Answers4

10

Yes, C++11 introduces something similar in std::to_string.

Before that, your safest bet was to use a std::stringstream, fill it with operator <<, and retrieve the string with .str().

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
5

C++11 added to_string functions to the standard library; check them out

Also, the Boost library has lexical_cast.

Or you can write your own, internally using either e.g. std::ostringstream (from <sstream>) or the C library's facilities. The latter is generally more efficient (in practice).

For the general concatenation + formatting expressions that you show, you should instead consider a general formatter that can do that in linear time.

A simple (not particularly efficient or feature-rich) such formatter is shown in full in another question's answer.

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
4

Templates

Code:

template <typename T>
std::string toString(const T &val_p)
{
    std::stringstream x;
    x << val_p;
    return x.str();  
}

This function will convert ANY type of object to a string, provided that the appropriate ostream insertion operator is defined. To convert custom objects, you merely write a function:

Code:

std::ostream &operator<<(std::ostream &stream_p, const my_class &val_p)
{
    // ...
    return stream_p;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • **A refinement**: To show that the intent is to **output** a string, replace `std::stringstream` with `std::ostringstream` in the above `template`. See [this question](http://stackoverflow.com/questions/3292107/whats-the-difference-between-istringstream-ostringstream-and-stringstream-w) for a relevant discussion. – DavidRR Feb 12 '13 at 20:50
2

Yes, C++11 has made to_string part of its language (as a free function).

However it does not replace using ostringstream, and if you are building a string of a lot of objects, it is better to continue using ostringstream rather than convert each item to a string and concatenate them.

The reason is that when you use ostringstream, it will create a buffer into which to write the output. When you call to_string then concatenate, it is likely to_string will create its own ostringstream object with its own buffer.

If users are going to be using operator+ to concatenate strings this will be very inefficient. If they create a big ostringstream to start with you may as well stream the objects in direct rather than call to_string on them first.

I actually foresee this being primarily used for logging and exception throwing. In the latter case efficiency probably doesn't matter.

(Code I have seen using boost's datetime library and streaming commonly suffers from the "create a new facet and locale for every date I print/parse" inefficiency. I have fixed that inefficiency in 2 companies where I have worked).

CashCow
  • 30,981
  • 5
  • 61
  • 92