如何在一行上连接多个C ++字符串? [英] How do I concatenate multiple C++ strings on one line?

查看:169
本文介绍了如何在一行上连接多个C ++字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#有一个语法功能,您可以在一行上将许多数据类型连接在一起。

C# has a syntax feature where you can concatenate many data types together on 1 line.

string s = new String();
s += "Hello world, " + myInt + niceToSeeYouString;
s += someChar1 + interestingDecimal + someChar2;

在C ++中会有什么等价的?就我所见,你必须在单独的行上做它,因为它不支持多个字符串/变量与+运算符。这是好的,但看起来不整洁。

What would be the equivalent in C++? As far as I can see, you'd have to do it all on separate lines as it doesn't support multiple strings/variables with the + operator. This is OK, but doesn't look as neat.

string s;
s += "Hello world, " + "nice to see you, " + "or not.";

上述代码产生错误。

推荐答案

#include <sstream>
#include <string>

std::stringstream ss;
ss << "Hello, world, " << myInt << niceToSeeYouString;
std::string s = ss.str();

看看这篇来自Herb Sutter的Guru Of The Week文章:庄园农场的字符串格式化

Take a look at this Guru Of The Week article from Herb Sutter: The String Formatters of Manor Farm

这篇关于如何在一行上连接多个C ++字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆