c++ - 你如何快速连接一个类似于连接字符串的整数

标签 c++ integer concatenation

<分区>

在 C++ 中是否有任何函数可以连接一个 int 或我可以编写的一小段代码来执行此操作。到目前为止我发现的一切似乎都不必要地复杂。我只是想知道,因为对于字符串,您只需将两个字符串加在一起,所以任何整数等价。

最佳答案

使用std::to_string

#include <string>
std::string s("helloworld:") + std::to_string(3);

输出:helloworld:3

或者您可以使用 stringstream 来归档您想要的内容

#include <sstream>
std::string s("helloworld:");

std::stringstream ss;
ss << 3;
s += ss.str();

输出:helloworld:3

关于c++ - 你如何快速连接一个类似于连接字符串的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17907321/

相关文章:

c++ - MFC 和 Kinect : draw a line on Kinect's image

c++ - Boost::asio 不在 Debian Wheezy 上?

c++ - 有符号整数类型的最大值

python - 如何在特定键上合并这两个数据帧?

c++ - sprintf 串联参数 : sprintf ( a+b, "")

c# - Xamarin 和 C++ 代码

ruby - Redis:我应该将 IP 转换为整数吗?

c# - 非常大的整数的快速乘法

在C中连接字符串,哪种方法更有效?

python - 有没有更好的方法来提高连接速度?