C#具有语法功能,您可以在一行中将多种数据类型连接在一起.
string s = new String(); s += "Hello world, " + myInt + niceToSeeYouString; s += someChar1 + interestingDecimal + someChar2;
什么是C++中的等价物?据我所知,你必须在单独的行上完成所有操作,因为它不支持使用+运算符的多个字符串/变量.这没关系,但看起来并不整洁.
string s; s += "Hello world, " + "nice to see you, " + "or not.";
上面的代码产生错误.
#include#include std::stringstream ss; ss << "Hello, world, " << myInt << niceToSeeYouString; std::string s = ss.str();
看看Herb Sutter撰写的本周大师文章:Manor Farm的String Formatters
5年没有人提到过.append
?
#includestd::string s; s.append("Hello world, "); s.append("nice to see you, "); s.append("or not.");
s += "Hello world, " + "nice to see you, " + "or not.";
那些字符数组文字不是C++ std :: strings - 你需要转换它们:
s += string("Hello world, ") + string("nice to see you, ") + string("or not.");
要转换int(或任何其他可流式类型),您可以使用boost lexical_cast或提供自己的函数:
templatestring Str( const T & t ) { ostringstream os; os << t; return os.str(); }
你现在可以这样说:
string s = "The meaning is " + Str( 42 );
您的代码可以写成1,
s = "Hello world," "nice to see you," "or not."
...但我怀疑这就是你要找的东西.在您的情况下,您可能正在寻找流:
std::stringstream ss; ss << "Hello world, " << 42 << "nice to see you."; std::string s = ss.str();
1 " 可写为 ":这仅适用于字符串文字.连接由编译器完成.
使用C++ 14用户定义的文字和std::to_string
代码变得更容易.
using namespace std::literals::string_literals; std::string str; str += "Hello World, "s + "nice to see you, "s + "or not"s; str += "Hello World, "s + std::to_string(my_int) + other_string;
请注意,可以在编译时完成串联字符串文字.只需删除+
.
str += "Hello World, " "nice to see you, " "or not";
提供更一行的解决方案:concat
可以实现一种功能,将基于"经典"字符串流的解决方案简化为单一语句.它基于可变参数模板和完美转发.
用法:
std::string s = concat(someObject, " Hello, ", 42, " I concatenate", anyStreamableType);
执行:
void addToStream(std::ostringstream&) { } templatevoid addToStream(std::ostringstream& a_stream, T&& a_value, Args&&... a_args) { a_stream << std::forward (a_value); addToStream(a_stream, std::forward (a_args)...); } template std::string concat(Args&&... a_args) { std::ostringstream s; addToStream(s, std::forward (a_args)...); return s.str(); }
提高::格式
或者std :: stringstream
std::stringstream msg; msg << "Hello world, " << myInt << niceToSeeYouString; msg.str(); // returns std::string object
在实际的问题是,串联字符串常量与+
在C++中失败:
string s;
s += "Hello world, " + "nice to see you, " + "or not.";
上面的代码产生错误.
在C++中(也在C语言中),只需将它们放在彼此旁边即可连接字符串文字:
string s0 = "Hello world, " "nice to see you, " "or not."; string s1 = "Hello world, " /*same*/ "nice to see you, " /*result*/ "or not."; string s2 = "Hello world, " /*line breaks in source code as well as*/ "nice to see you, " /*comments don't matter*/ "or not.";
如果您在宏中生成代码,这是有道理的:
#define TRACE(arg) cout << #arg ":" << (arg) << endl;
...一个可以像这样使用的简单宏
int a = 5; TRACE(a) a += 7; TRACE(a) TRACE(a+7) TRACE(17*11)
(现场演示......)
或者,如果你坚持使用+
for字符串文字(正如underscore_d已经建议的那样):
string s = string("Hello world, ")+"nice to see you, "+"or not.";
另一个解决方案是const char*
为每个连接步骤组合一个字符串和一个
string s; s += "Hello world, " s += "nice to see you, " s += "or not.";
auto s = string("one").append("two").append("three")
使用{fmt}库,您可以执行以下操作:
auto s = fmt::format("{}{}{}", "Hello world, ", myInt, niceToSeeYouString);
建议将该库的子集标准化为P0645文本格式,如果被接受,则以上内容将变为:
auto s = std::format("{}{}{}", "Hello world, ", myInt, niceToSeeYouString);
免责声明:我是{fmt}库的作者。