【问题标题】:what is the cleanest way to concat a const char * with a string in c++?在 C++ 中将 const char * 与字符串连接的最干净的方法是什么?
【发布时间】:2014-07-19 19:04:16
【问题描述】:

我知道我可以使用stringstream,但我希望有一种更简单的方法来编写"hello" + a_std_string_object

【问题讨论】:

  • "hello" + a_std_string_object 应该按书面方式工作。你有问题吗?
  • a_std_string_object.insert(0, "hello");

标签: c++ string stl


【解决方案1】:

如果您有旧的/损坏的编译器,则可能需要

string("hello") + std_string_object

但在现代编译器中,自然代码

"hello" + std_string_object

应该可以。

【讨论】:

  • @Chad 好的,我过去肯定使用过自然方式不起作用的编译器。我从来没有注意到它得到了修复。无论如何,这不是经常出现的事情。
  • "hello" + std_string_object 至少从 C++98 开始就有效。也许您正在考虑使用 const char * 添加字符串文字。
  • 我在考虑 g++ 2.95。那个编译器没有做任何实现 C++98 的借口:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 2014-03-19
  • 1970-01-01
  • 2013-03-23
相关资源
最近更新 更多