<pre><code>int i = 4;
string text = "Player ";
cout &lt;&lt; (text + i);
</code></pre>

<p>I'd like it to print <code>Player 4</code>.</p>

<p>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?</p>

[{'answer_id': 64794, 'author': 'rupello', 'author_id': 635, 'author_profile': 'https://Stackoverflow.com/users/635', 'pm_score': 0, 'selected': False, 'text': '<pre><code>cout &lt;&lt; "Player" &lt;&lt; i ;\n</code></pre>\n'}, {'answer_id': 64795, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<pre><code>cout &lt;&lt; text &lt;&lt; i;\n</code></pre>\n'}, {'answer_id': 64796, 'author': 'jjnguy', 'author_id': 2598, 'author_profile': 'https://Stackoverflow.com/users/2598', 'pm_score': 3, 'selected': False, 'text': '<pre><code>cout &lt;&lt; text &lt;&lt; " " &lt;&lt; i &lt;&lt; endl;\n</code></pre>\n'}, {'answer_id': 64797, 'author': 'introp', 'author_id': 8398, 'author_profile': 'https://Stackoverflow.com/users/8398', 'pm_score': 0, 'selected': False, 'text': '<pre><code>cout &lt;&lt; text &lt;&lt; i;\n</code></pre>\n\n<p>The <code>&lt;&lt;</code> operator for ostream returns a reference to the ostream, so you can just keep chaining the <code>&lt;&lt;</code> operations.  That is, the above is basically the same as:</p>\n\n<pre><code>cout &lt;&lt; text;\ncout &lt;&lt; i;\n</code></pre>\n'}, {'answer_id': 64804, 'author': 'GEOCHET', 'author_id': 5640, 'author_profile': 'https://Stackoverflow.com/users/5640', 'pm_score': 0, 'selected': False, 'text': '<pre><code>cout &lt;&lt; text &lt;&lt; " " &lt;&lt; i &lt;&lt; endl;\n</code></pre>\n'}, {'answer_id': 64805, 'author': 'Jason Baker', 'author_id': 2147, 'author_profile': 'https://Stackoverflow.com/users/2147', 'pm_score': 3, 'selected': False, 'text': '<p>For the record, you can also use a <a href="http://www.cplusplus.com/reference/iostream/stringstream/stringstream.html" rel="nofollow noreferrer"><code>std::stringstream</code></a> if you want to create the string before it\'s actually output.</p>\n'}, {'answer_id': 64811, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 4, 'selected': False, 'text': "<p>These work for general strings (in case you do not want to output to file/console, but store for later use or something).</p>\n\n<p>boost.lexical_cast</p>\n\n<pre><code>MyStr += boost::lexical_cast&lt;std::string&gt;(MyInt);\n</code></pre>\n\n<p>String streams</p>\n\n<pre><code>//sstream.h\nstd::stringstream Stream;\nStream.str(MyStr);\nStream &lt;&lt; MyInt;\nMyStr = Stream.str();\n\n// If you're using a stream (for example, cout), rather than std::string\nsomeStream &lt;&lt; MyInt;\n</code></pre>\n"}, {'answer_id': 64815, 'author': 'Eric', 'author_id': 4540, 'author_profile': 'https://Stackoverflow.com/users/4540', 'pm_score': 7, 'selected': False, 'text': '<pre><code>printf("Player %d", i);\n</code></pre>\n\n<p>(Downvote my answer all you like; I still hate the C++ I/O operators.)</p>\n\n<p>:-P</p>\n'}, {'answer_id': 64817, 'author': 'Sebastian Redl', 'author_id': 8922, 'author_profile': 'https://Stackoverflow.com/users/8922', 'pm_score': 8, 'selected': False, 'text': '<p>Well, if you use cout you can just write the integer directly to it, as in</p>\n\n<pre><code>std::cout &lt;&lt; text &lt;&lt; i;\n</code></pre>\n\n<p>The C++ way of converting all kinds of objects to strings is through <a href="http://en.cppreference.com/w/cpp/io/basic_stringstream" rel="noreferrer">string streams</a>. If you don\'t have one handy, just create one.</p>\n\n<pre><code>#include &lt;sstream&gt;\n\nstd::ostringstream oss;\noss &lt;&lt; text &lt;&lt; i;\nstd::cout &lt;&lt; oss.str();\n</code></pre>\n\n<p>Alternatively, you can just convert the integer and append it to the string.</p>\n\n<pre><code>oss &lt;&lt; i;\ntext += oss.str();\n</code></pre>\n\n<p>Finally, the Boost libraries provide <a href="http://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast.html" rel="noreferrer"><code>boost::lexical_cast</code></a>, which wraps around the stringstream conversion with a syntax like the built-in type casts.</p>\n\n<pre><code>#include &lt;boost/lexical_cast.hpp&gt;\n\ntext += boost::lexical_cast&lt;std::string&gt;(i);\n</code></pre>\n\n<p>This also works the other way around, i.e. to parse strings.</p>\n'}, {'answer_id': 65309, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>There are a few options, and which one you want depends on the context.</p>\n\n<p>The simplest way is</p>\n\n<pre><code>std::cout &lt;&lt; text &lt;&lt; i;\n</code></pre>\n\n<p>or if you want this on a single line</p>\n\n<pre><code>std::cout &lt;&lt; text &lt;&lt; i &lt;&lt; endl;\n</code></pre>\n\n<p>If you are writing a single threaded program and if you aren\'t calling this code a lot (where "a lot" is thousands of times per second) then you are done.</p>\n\n<p>If you are writing a multi threaded program and more than one thread is writing to cout, then this simple code can get you into trouble. Let\'s assume that the library that came with your compiler made cout thread safe enough than any single call to it won\'t be interrupted. Now let\'s say that one thread is using this code to write "Player 1" and another is writing "Player 2". If you are lucky you will get the following:</p>\n\n<pre><code>Player 1\nPlayer 2\n</code></pre>\n\n<p>If you are unlucky you might get something like the following</p>\n\n<pre><code>Player Player 2\n1\n</code></pre>\n\n<p>The problem is that std::cout &lt;&lt; text &lt;&lt; i &lt;&lt; endl; turns into 3 function calls. The code is equivalent to the following:</p>\n\n<pre><code>std::cout &lt;&lt; text;\nstd::cout &lt;&lt; i;\nstd::cout &lt;&lt; endl;\n</code></pre>\n\n<p>If instead you used the C-style printf, and again your compiler provided a runtime library with reasonable thread safety (each function call is atomic) then the following code would work better:</p>\n\n<pre><code>printf("Player %d\\n", i);\n</code></pre>\n\n<p>Being able to do something in a single function call lets the io library provide synchronization under the covers, and now your whole line of text will be atomically written.</p>\n\n<p>For simple programs, std::cout is great. Throw in multithreading or other complications and the less stylish printf starts to look more attractive.</p>\n'}, {'answer_id': 941229, 'author': 'Daniel James', 'author_id': 2434, 'author_profile': 'https://Stackoverflow.com/users/2434', 'pm_score': 2, 'selected': False, 'text': '<p>Another possibility is <a href="http://www.boost.org/doc/libs/release/libs/format/doc/format.html" rel="nofollow noreferrer">Boost.Format</a>:</p>\n\n<pre><code>#include &lt;boost/format.hpp&gt;\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n\nint main() {\n  int i = 4;\n  std::string text = "Player";\n  std::cout &lt;&lt; boost::format("%1% %2%\\n") % text % i;\n}\n</code></pre>\n'}, {'answer_id': 6455164, 'author': 'Brian Lenoski', 'author_id': 577587, 'author_profile': 'https://Stackoverflow.com/users/577587', 'pm_score': 2, 'selected': False, 'text': '<p>For the record, you could also use Qt\'s <code>QString</code> class:</p>\n\n<pre><code>#include &lt;QtCore/QString&gt;\n\nint i = 4;\nQString qs = QString("Player %1").arg(i);\nstd::cout &lt;&lt; qs.toLocal8bit().constData();  // prints "Player 4"\n</code></pre>\n'}, {'answer_id': 6490852, 'author': 'Mr. Y', 'author_id': 817063, 'author_profile': 'https://Stackoverflow.com/users/817063', 'pm_score': -1, 'selected': False, 'text': '<p>If using Windows/MFC, and need the string for more than immediate output try:</p>\n\n<pre><code>int i = 4;\nCString strOutput;\nstrOutput.Format("Player %d", i);\n</code></pre>\n'}, {'answer_id': 6705708, 'author': 'Robert Parcus', 'author_id': 814180, 'author_profile': 'https://Stackoverflow.com/users/814180', 'pm_score': 2, 'selected': False, 'text': '<p>Here a small working conversion/appending example, with some code I needed before.</p>\n\n<pre><code>#include &lt;string&gt;\n#include &lt;sstream&gt;\n#include &lt;iostream&gt;\n\nusing namespace std;\n\nint main(){\nstring str;\nint i = 321;\nstd::stringstream ss;\nss &lt;&lt; 123;\nstr = "/dev/video";\ncout &lt;&lt; str &lt;&lt; endl;\ncout &lt;&lt; str &lt;&lt; 456 &lt;&lt; endl;\ncout &lt;&lt; str &lt;&lt; i &lt;&lt; endl;\nstr += ss.str();\ncout &lt;&lt; str &lt;&lt; endl;\n}\n</code></pre>\n\n<p>the output will be:</p>\n\n<pre><code>/dev/video\n/dev/video456\n/dev/video321\n/dev/video123\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 15182224, 'author': 'user2037225', 'author_id': 2037225, 'author_profile': 'https://Stackoverflow.com/users/2037225', 'pm_score': -1, 'selected': False, 'text': '<p>You can use the following</p>\n\n<pre><code>int i = 4;\nstring text = "Player ";\ntext+=(i+\'0\');\ncout &lt;&lt; (text);\n</code></pre>\n'}, {'answer_id': 16600810, 'author': 'Richard', 'author_id': 752843, 'author_profile': 'https://Stackoverflow.com/users/752843', 'pm_score': 3, 'selected': False, 'text': '<p>Your example seems to indicate that you would like to display the a string followed by an integer, in which case:</p>\n\n<pre><code>string text = "Player: ";\nint i = 4;\ncout &lt;&lt; text &lt;&lt; i &lt;&lt; endl;\n</code></pre>\n\n<p>would work fine.</p>\n\n<p>But, if you\'re going to be storing the string places or passing it around, and doing this frequently, you may benefit from overloading the addition operator. I demonstrate this below:</p>\n\n<pre><code>#include &lt;sstream&gt;\n#include &lt;iostream&gt;\nusing namespace std;\n\nstd::string operator+(std::string const &amp;a, int b) {\n  std::ostringstream oss;\n  oss &lt;&lt; a &lt;&lt; b;\n  return oss.str();\n}\n\nint main() {\n  int i = 4;\n  string text = "Player: ";\n  cout &lt;&lt; (text + i) &lt;&lt; endl;\n}\n</code></pre>\n\n<p>In fact, you can use templates to make this approach more powerful:</p>\n\n<pre><code>template &lt;class T&gt;\nstd::string operator+(std::string const &amp;a, const T &amp;b){\n  std::ostringstream oss;\n  oss &lt;&lt; a &lt;&lt; b;\n  return oss.str();\n}\n</code></pre>\n\n<p>Now, as long as object <code>b</code> has a defined stream output, you can append it to your string (or, at least, a copy thereof).</p>\n'}, {'answer_id': 18997915, 'author': 'headmyshoulder', 'author_id': 625476, 'author_profile': 'https://Stackoverflow.com/users/625476', 'pm_score': 8, 'selected': False, 'text': '<p>With C++11, you can write:</p>\n\n<pre><code>#include &lt;string&gt;     // to use std::string, std::to_string() and "+" operator acting on strings \n\nint i = 4;\nstd::string text = "Player ";\ntext += std::to_string(i);\n</code></pre>\n'}, {'answer_id': 32389356, 'author': 'Saurabh Mishra', 'author_id': 5299089, 'author_profile': 'https://Stackoverflow.com/users/5299089', 'pm_score': 1, 'selected': False, 'text': '<p>One method here is directly printing the output if its required in your problem.</p>\n\n<pre><code>cout &lt;&lt; text &lt;&lt; i;\n</code></pre>\n\n<p>Else, one of the safest method is to use </p>\n\n<pre><code>sprintf(count, "%d", i);\n</code></pre>\n\n<p>And then copy it to your "text" string .</p>\n\n<pre><code>for(k = 0; *(count + k); k++)\n{ \n  text += count[k]; \n} \n</code></pre>\n\n<p>Thus, you have your required output string </p>\n\n<p>For more info on <code>sprintf</code>, follow:\n<a href="http://www.cplusplus.com/reference/cstdio/sprintf" rel="nofollow">http://www.cplusplus.com/reference/cstdio/sprintf</a></p>\n'}, {'answer_id': 33784419, 'author': 'Code Black', 'author_id': 5367912, 'author_profile': 'https://Stackoverflow.com/users/5367912', 'pm_score': 0, 'selected': False, 'text': '<p><strong>The easiest way I could figure this out is the following..</strong><br>\nIt will work as a <strong>single string</strong> and <strong>string array</strong>.\nI am considering a string array, as it is complicated (little bit same will be followed with string).\nI create a array of names and <strong>append</strong> some integer and char with it to show how easy it is to append some <strong>int</strong> and <strong>chars</strong> to string, hope it helps.\nlength is just to measure the size of array. If you are familiar with programming then <strong>size_t</strong> is a unsigned <strong>int</strong></p>\n\n<pre><code>#include&lt;iostream&gt;\n    #include&lt;string&gt;\n    using namespace std;\n    int main() {\n\n        string names[] = { "amz","Waq","Mon","Sam","Has","Shak","GBy" }; //simple array\n        int length = sizeof(names) / sizeof(names[0]); //give you size of array\n        int id;\n        string append[7];    //as length is 7 just for sake of storing and printing output \n        for (size_t i = 0; i &lt; length; i++) {\n            id = rand() % 20000 + 2;\n            append[i] = names[i] + to_string(id);\n        }\n        for (size_t i = 0; i &lt; length; i++) {\n            cout &lt;&lt; append[i] &lt;&lt; endl;\n        }\n\n\n}\n</code></pre>\n'}, {'answer_id': 37435228, 'author': 'Евгений Яковлев', 'author_id': 6380629, 'author_profile': 'https://Stackoverflow.com/users/6380629', 'pm_score': -1, 'selected': False, 'text': '<p>You also try concatenate player\'s number with <code>std::string::push_back</code> :</p>\n\n<p>Example with your code:</p>\n\n<pre><code>int i = 4;\nstring text = "Player ";\ntext.push_back(i + \'0\');\ncout &lt;&lt; text;\n</code></pre>\n\n<p>You will see in console: </p>\n\n<blockquote>\n  <p>Player 4</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/64782', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']