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.

Upvotes: 2

Views: 12482

Answers (9)

Some programmer dude
Some programmer dude

Reputation: 409364

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.

Upvotes: 8

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).

Upvotes: 0

hmjd
hmjd

Reputation: 122001

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().

Upvotes: 6

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24153

Encapsulate:

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

Then:

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

Upvotes: 4

Pete Becker
Pete Becker

Reputation: 76438

Simple enough:

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

Upvotes: 5

SinisterMJ
SinisterMJ

Reputation: 3509

You could use strcat() as well

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

Upvotes: 1

klm123
klm123

Reputation: 12885

Be simple:

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

Upvotes: 2

Matteo Italia
Matteo Italia

Reputation: 126867

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.

Upvotes: 2

Alexander Putilin
Alexander Putilin

Reputation: 2342

I would use std::stringstream:

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

stringstream reference

Upvotes: 2

Related Questions