TAGS :Viewed: 6 - Published at: a few seconds ago

[ Concatenate a string and a boolean in C++? ]

I'd like to do something like the following:

bool b = ...
string s = "Value of bool is: " + b ? "f" : "d";

All of the examples I've seen use cout, but I don't want to print the string; just store it.

How do I do it? If possible, I'd like one example that assigns to a char * and one to a std::string.

Answer 1


If your compiler is new enough, it should have std::to_string:

string s = "Value of bool is: " + std::to_string(b);

This would of course append "1" (for true) or "0" (for false) to your string, not "f" or "d" as you want. The reason being that ther is no overload of std::to_string that takes a bool type, so the compiler converts it to an integer value.

You can of course do it in two step, first declare the string then append the value:

string s = "Value of bool is: ";
s += b ? "f" : "d";

Or do it almost like you do now, but explicitly create the second as a std::string:

string s = "Value of bool is: " + std::string(b ? "f" : "d");

Edit: How to get a char pointer from a std::string

This is done with the std::string::c_str method. But as noted by Pete Becker you have to be careful how you use this pointer, as it points to data inside the string object. If the object is destroyed so is the data, and the pointer, if saved, will now be invalid.

Answer 2


Use ostringstream:

std::ostringstream s;
s << "Value of bool is: " << b;
std::string str(s.str());

and you can use std::boolalpha to have "true" or "false" instead of an int representation:

s << std::boolalpha << "Value of bool is: " << b;

Note the posted code is almost correct (it is not possible to + two char[]):

std::string s = std::string("Value of bool is: ") + (b ? "t" : "f");

To assign to a char[] you could use snprintf():

char buf[1024];
std::snprintf(buf, 1024, "Value of bool is: %c", b ? 't' : 'f');

or just std::string::c_str().

Answer 3


Simple enough:

std::string s = std::string("Value of bool is: ") + (b ? "f" : "d");

Answer 4


I would use std::stringstream:

std::stringstream ss;
ss << s << (b ? "f" : "d");
std::string resulting_string = ss.str();

stringstream reference

Answer 5


Perform the operation in two steps:

bool b = ...
string s = "Value of bool is: ";
s+= b ? "f" : "d";

this is necessary because otherwise you would be trying to sum two const char *, which is not permitted; this way, instead, you rely on the overload of += operator for std::string and C strings.

Answer 6


Be simple:

bool b = ...
string s = "Value of bool is: ";
if (b)
  s += "f";
else
  s += "d";

Answer 7


You could use strcat() as well

char s[80];
strcpy(s, "Value of bool is ");
strcat(s, b?"f":"d");

Answer 8


Encapsulate:

std::string toString(const bool value)
{
    return value ? "true" : "false";
}

Then:

std::string text = "Value of bool is: " + toString(b);

Answer 9


For this simple use case, just append one string to the other:

std::string text = std::string("Value of bool is: ").append( value? "true" : "false" ); 

Now, for a more generic solution you can create a string builder class:

class make_string {
   std::ostringstream st;
   template <typename T>
   make_string& operator()( T const & v ) {
       st << v;
   }
   operator std::string() {
       return st.str();
   }
};

Which can be easily extended to support manipulators (adding a couple of extra overloads), but this would suffice for most basic uses. Then use it as:

std::string msg = make_string() << "Value of bool is " << (value?"true":"false");

(Again, in this particular case it is overkill, but if you want to compose a more complex string this would be helpful).