How do you append an int to a string in C++?

Wawa picture Wawa · Sep 15, 2008 · Viewed 462.7k times · Source
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?

Answer

headmyshoulder picture headmyshoulder · Sep 25, 2013

With C++11, you can write:

#include <string>     // to use std::string, std::to_string() and "+" operator acting on strings 

int i = 4;
std::string text = "Player ";
text += std::to_string(i);