我如何返回C++中的变量和字符的字符串?
我有一个字符串功能,我想输出以下代表行。
string print_ticket(void){
if(sold_status == true){
cout<<seat_number<<" "<<seat_number<<"sold";
}
else{
cout<<seat_number<<" "<<seat_number<<"available";
}
}
问题是该功能必须返回字符串,我不确定在这种情况下将这些Cout语句转换为字符串的最佳方法。
谢谢您的任何帮助。
I have a string function that I would like to output the following cout lines.
string print_ticket(void){
if(sold_status == true){
cout<<seat_number<<" "<<seat_number<<"sold";
}
else{
cout<<seat_number<<" "<<seat_number<<"available";
}
}
The problem is the function must return a string and I'm not sure the best way to turn these cout statement into a string in this scenario.
Thank you for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
OSTRINGSTREAM
,包括&lt; sstream&gt;
:Use
ostringstream
, available when including<sstream>
:德里克的答案可以简化一些。可以将普通的旧字符串连接(也可以链接),因此您可以将其降低到:
哪个更有效?好吧,这取决于。
Derek's answer can be simplified somewhat. Plain old strings can be concatenated (and also chained), so you can reduce it to:
Which is more efficient? Well, it depends.