如何在 C++ 中将 int 附加到字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/64782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:35:50  来源:igfitidea点击:

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

c++intstdstring

提问by Sebastian Redl

int i = 4;
string text = "Player ";
cout << (text + i);

I'd like it to print Player 4.

我想打印出来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?

以上显然是错误的,但它显示了我在这里尝试做的事情。有没有一种简单的方法可以做到这一点,还是我必须开始添加新的包含?

回答by headmyshoulder

With C++11, you can write:

使用 C++11,您可以编写:

#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);

回答by Sebastian Redl

Well, if you use cout you can just write the integer directly to it, as in

好吧,如果您使用 cout,您可以直接将整数写入它,如

std::cout << text << i;

The C++ way of converting all kinds of objects to strings is through string streams. If you don't have one handy, just create one.

将各种对象转换为字符串的 C++ 方式是通过字符串流。如果你手头没有,那就创建一个。

#include <sstream>

std::ostringstream oss;
oss << text << i;
std::cout << oss.str();

Alternatively, you can just convert the integer and append it to the string.

或者,您可以只转换整数并将其附加到字符串。

oss << i;
text += oss.str();

Finally, the Boost libraries provide boost::lexical_cast, which wraps around the stringstream conversion with a syntax like the built-in type casts.

最后,Boost 库提供了boost::lexical_cast,它使用类似于内置类型转换的语法围绕字符串流转换。

#include <boost/lexical_cast.hpp>

text += boost::lexical_cast<std::string>(i);

This also works the other way around, i.e. to parse strings.

这也适用于其他方式,即解析字符串。

回答by Eric

printf("Player %d", i);

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

(你喜欢什么就对我的答案投反对票;我仍然讨厌 C++ I/O 操作符。)

:-P

:-P

回答by Fire Lancer

These work for general strings (in case you do not want to output to file/console, but store for later use or something).

这些适用于一般字符串(如果您不想输出到文件/控制台,而是存储以供以后使用或其他东西)。

boost.lexical_cast

boost.lexical_cast

MyStr += boost::lexical_cast<std::string>(MyInt);

String streams

字符串流

//sstream.h
std::stringstream Stream;
Stream.str(MyStr);
Stream << MyInt;
MyStr = Stream.str();

// If you're using a stream (for example, cout), rather than std::string
someStream << MyInt;

回答by Jason Baker

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

作为记录,std::stringstream如果您想在实际输出之前创建字符串,也可以使用 a 。

回答by jjnguy

cout << text << " " << i << endl;

回答by Richard

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 bhas a defined stream output, you can append it to your string (or, at least, a copy thereof).

现在,只要对象b具有定义的流输出,您就可以将其附加到您的字符串(或至少是其副本)。

回答by Daniel James

Another possibility is Boost.Format:

另一种可能性是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;
}

回答by Brian Lenoski

For the record, you could also use Qt's QStringclass:

作为记录,您还可以使用 Qt 的QString类:

#include <QtCore/QString>

int i = 4;
QString qs = QString("Player %1").arg(i);
std::cout << qs.toLocal8bit().constData();  // prints "Player 4"

回答by Robert Parcus

Here a small working conversion/appending example, with some code I needed before.

这是一个小的工作转换/附加示例,其中包含我以前需要的一些代码。

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main(){
string str;
int i = 321;
std::stringstream ss;
ss << 123;
str = "/dev/video";
cout << str << endl;
cout << str << 456 << endl;
cout << str << i << endl;
str += ss.str();
cout << str << endl;
}

the output will be:

输出将是:

/dev/video
/dev/video456
/dev/video321
/dev/video123

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.

请注意,在最后两行中,您在实际打印出来之前保存了修改后的字符串,如果需要,您可以稍后使用它。