【发布时间】:2016-11-04 06:53:41
【问题描述】:
假设我有一个 std::string 对象和一个以 null 结尾的字符数组(或 C 样式字符串):
std::string str("This is a ");
const char* cstr = "strings are a really important data type.";
如何以最简洁和最有效的方式将 C 样式字符串的前 N 个字符(在本例中为 6 个,以便 str 将包含 This is a string)附加到 std::string?
【问题讨论】:
-
你不会相信,但是有一个函数叫做
append,即str.append(cstr, 6); -
@PiotrSkotnicki 我真傻。出于某种原因,我认为
append仅适用于单个字符。
标签: c++ string c++11 string-concatenation c-strings