c++ - 你如何在 C++ 中将一个 int 附加到一个字符串?

标签 c++ int stdstring

<分区>

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

我希望它打印Player 4

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

最佳答案

使用 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);

关于c++ - 你如何在 C++ 中将一个 int 附加到一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41168030/

相关文章:

Java 8 Streams int 和 Integer

c++ - 为什么 std::string 没有(明确的)const char* cast

c++ - C++ 中的 pthread_create 错误(类内的 pthread)

c++ - 检测类型的存在

c++ - 创建对象时是否初始化类中的成员值?

java - 将整数文字分配给Java中的 double 变量

java - Java 中的 16 位桶式移位

c++ - std::string 如何管理这个技巧?

c++ - 为什么 b[2] 为假?

c++ - 使用 cmake 和 mingw 在 mac OSX 上交叉编译 dll