gpt4 book ai didi

c++ - 不能直接在 C++ 中支持连接的 int 和 string?

转载 作者:太空宇宙 更新时间:2023-11-04 15:42:17 25 4
gpt4 key购买 nike

int x=10;
string str;
str+=x;
cout<<"hello"<<str<<endl;

为什么只打印 hello 而不是 hello10?

不能在 C++ 中连接 int 和 string 吗?

最佳答案

您需要使用 stringstream对于这种需求。

int x=10;
stringstream ss;//create a stringstream
ss << x;//add number to the stream
string str=ss.str();
cout<<"hello"<<str<<endl;//Will print hello10
return 0;

关于c++ - 不能直接在 C++ 中支持连接的 int 和 string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com