This question already has an answer here:
- How to concatenate a std::string and an int? 29 answers
int i = 4;
string text = "Player ";
cout << (text + i);
I'd like it to print Player 4
.
The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes?
The
<<
operator for ostream returns a reference to the ostream, so you can just keep chaining the<<
operations. That is, the above is basically the same as:For the record, you could also use Qt's
QString
class:The easiest way I could figure this out is the following..
It will work as a single string and string array. I am considering a string array, as it is complicated (little bit same will be followed with string). I create a array of names and append some integer and char with it to show how easy it is to append some int and chars to string, hope it helps. length is just to measure the size of array. If you are familiar with programming then size_t is a unsigned int
If using Windows/MFC, and need the string for more than immediate output try:
You can use the following
Here a small working conversion/appending example, with some code I needed before.
the output will be:
Note that in the last two lines you save the modified string before it's actually printed out, and you could use it later if needed.