Nic*_*ton 136 c++ string compiler-errors concatenation
C#具有语法功能,您可以在一行中将多种数据类型连接在一起.
string s = new String();
s += "Hello world, " + myInt + niceToSeeYouString;
s += someChar1 + interestingDecimal + someChar2;
Run Code Online (Sandbox Code Playgroud)
什么是C++中的等价物?据我所知,你必须在单独的行上完成所有操作,因为它不支持使用+运算符的多个字符串/变量.这没关系,但看起来并不整洁.
string s;
s += "Hello world, " + "nice to see you, " + "or not.";
Run Code Online (Sandbox Code Playgroud)
上面的代码产生错误.
Pao*_*sco 226
#include <sstream>
#include <string>
std::stringstream ss;
ss << "Hello, world, " << myInt << niceToSeeYouString;
std::string s = ss.str();
Run Code Online (Sandbox Code Playgroud)
看看Herb Sutter撰写的本周大师文章:Manor Farm的String Formatters
Mic*_*hel 63
5年没有人提到过.append?
#include <string>
std::string s;
s.append("Hello world, ");
s.append("nice to see you, ");
s.append("or not.");
Run Code Online (Sandbox Code Playgroud)
小智 59
s += "Hello world, " + "nice to see you, " + "or not.";
Run Code Online (Sandbox Code Playgroud)
那些字符数组文字不是C++ std :: strings - 你需要转换它们:
s += string("Hello world, ") + string("nice to see you, ") + string("or not.");
Run Code Online (Sandbox Code Playgroud)
要转换int(或任何其他可流式类型),您可以使用boost lexical_cast或提供自己的函数:
template <typename T>
string Str( const T & t ) {
ostringstream os;
os << t;
return os.str();
}
Run Code Online (Sandbox Code Playgroud)
你现在可以这样说:
string s = "The meaning is " + Str( 42 );
Run Code Online (Sandbox Code Playgroud)
Joh*_*ing 40
您的代码可以写成1,
s = "Hello world," "nice to see you," "or not."
Run Code Online (Sandbox Code Playgroud)
...但我怀疑这就是你要找的东西.在您的情况下,您可能正在寻找流:
std::stringstream ss;
ss << "Hello world, " << 42 << "nice to see you.";
std::string s = ss.str();
Run Code Online (Sandbox Code Playgroud)
1 " 可写为 ":这仅适用于字符串文字.连接由编译器完成.
Rap*_*ptz 24
使用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;
Run Code Online (Sandbox Code Playgroud)
请注意,可以在编译时完成串联字符串文字.只需删除+.
str += "Hello World, " "nice to see you, " "or not";
Run Code Online (Sandbox Code Playgroud)
Seb*_*anK 16
提供更一行的解决方案:concat可以实现一种功能,将基于"经典"字符串流的解决方案简化为单一语句.它基于可变参数模板和完美转发.
用法:
std::string s = concat(someObject, " Hello, ", 42, " I concatenate", anyStreamableType);
Run Code Online (Sandbox Code Playgroud)
执行:
void addToStream(std::ostringstream&)
{
}
template<typename T, typename... Args>
void addToStream(std::ostringstream& a_stream, T&& a_value, Args&&... a_args)
{
a_stream << std::forward<T>(a_value);
addToStream(a_stream, std::forward<Args>(a_args)...);
}
template<typename... Args>
std::string concat(Args&&... a_args)
{
std::ostringstream s;
addToStream(s, std::forward<Args>(a_args)...);
return s.str();
}
Run Code Online (Sandbox Code Playgroud)
提高::格式
或者std :: stringstream
std::stringstream msg;
msg << "Hello world, " << myInt << niceToSeeYouString;
msg.str(); // returns std::string object
Run Code Online (Sandbox Code Playgroud)
在实际的问题是,串联字符串常量与+在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.";
Run Code Online (Sandbox Code Playgroud)
如果您在宏中生成代码,这是有道理的:
#define TRACE(arg) cout << #arg ":" << (arg) << endl;
Run Code Online (Sandbox Code Playgroud)
...一个可以像这样使用的简单宏
int a = 5;
TRACE(a)
a += 7;
TRACE(a)
TRACE(a+7)
TRACE(17*11)
Run Code Online (Sandbox Code Playgroud)
或者,如果你坚持使用+for字符串文字(正如underscore_d已经建议的那样):
string s = string("Hello world, ")+"nice to see you, "+"or not.";
Run Code Online (Sandbox Code Playgroud)
另一个解决方案是const char*为每个连接步骤组合一个字符串和一个
string s;
s += "Hello world, "
s += "nice to see you, "
s += "or not.";
Run Code Online (Sandbox Code Playgroud)
使用{fmt}库,您可以执行以下操作:
auto s = fmt::format("{}{}{}", "Hello world, ", myInt, niceToSeeYouString);
Run Code Online (Sandbox Code Playgroud)
建议将该库的子集标准化为P0645文本格式,如果被接受,则以上内容将变为:
auto s = std::format("{}{}{}", "Hello world, ", myInt, niceToSeeYouString);
Run Code Online (Sandbox Code Playgroud)
免责声明:我是{fmt}库的作者。