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

2019-01-08 04:12发布

This question already has an answer here:

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?

20条回答
爷的心禁止访问
2楼-- · 2019-01-08 04:49

Another possibility is Boost.Format:

#include <boost/format.hpp>
#include <iostream>
#include <string>

int main() {
  int i = 4;
  std::string text = "Player";
  std::cout << boost::format("%1% %2%\n") % text % i;
}
查看更多
虎瘦雄心在
3楼-- · 2019-01-08 04:50

Your example seems to indicate that you would like to display the a string followed by an integer, in which case:

string text = "Player: ";
int i = 4;
cout << text << i << endl;

would work fine.

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:

#include <sstream>
#include <iostream>
using namespace std;

std::string operator+(std::string const &a, int b) {
  std::ostringstream oss;
  oss << a << b;
  return oss.str();
}

int main() {
  int i = 4;
  string text = "Player: ";
  cout << (text + i) << endl;
}

In fact, you can use templates to make this approach more powerful:

template <class T>
std::string operator+(std::string const &a, const T &b){
  std::ostringstream oss;
  oss << a << b;
  return oss.str();
}

Now, as long as object b has a defined stream output, you can append it to your string (or, at least, a copy thereof).

查看更多
▲ chillily
4楼-- · 2019-01-08 04:52
cout << "Player" << i ;
查看更多
劫难
5楼-- · 2019-01-08 04:55
printf("Player %d", i);

(Downvote my answer all you like; I still hate the C++ I/O operators.)

:-P

查看更多
时光不老,我们不散
6楼-- · 2019-01-08 04:56

There are a few options, and which one you want depends on the context.

The simplest way is

std::cout << text << i;

or if you want this on a single line

std::cout << text << i << endl;

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.

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:

Player 1
Player 2

If you are unlucky you might get something like the following

Player Player 2
1

The problem is that std::cout << text << i << endl; turns into 3 function calls. The code is equivalent to the following:

std::cout << text;
std::cout << i;
std::cout << endl;

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:

printf("Player %d\n", i);

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.

For simple programs, std::cout is great. Throw in multithreading or other complications and the less stylish printf starts to look more attractive.

查看更多
时光不老,我们不散
7楼-- · 2019-01-08 04:57

For the record, you can also use a std::stringstream if you want to create the string before it's actually output.

查看更多
登录 后发表回答